I needed to reload an HTML page, internally from JavaScript, but didn't want to lose the inputs the user might have made. A browser's page reload will remove any JS code and context completely. There is no global variable or hidden element that survives a page reload where I could store my settings into.
URL parameters seem to be the only way to overcome such a situation. The following shows some JS code that provides saving and restoring of input fields using URL parameters.
The HTML code below exposes a "Layout"
button that executes the JS function pageScript.toggleLayout()
when the user clicks it. The two checkboxes with id="calculateMaxiumum"
and id="logging"
are to customize the behavior of the layout()
function, and should be preserved across the page reload. (For the real-world example look at my demo page.)
<div>
<input type="button" value="Layout" onclick="pageScript.toggleLayout(this);"/>
<br/>
<input type="checkbox" id="calculateMaximum" checked="bydefault">Use maximum width</input>
<br/>
<input type="checkbox" id="logging">Log to console</input>
</div>
And below is the code of the pageScript.toggleLayout()
function. When first called, it changes the layout by calling the internal layout()
function. It then changes to a "layouted" state, and next time it is clicked it reloads the page to restore the former layout. In this case it tries to keep the values of the checkboxes.
1 | <script type="text/javascript"> |
Mind that I use jQuery in this code, all $(...)
expressions are DOM queries via CSS expressions.
Mind further that this applies the "Revealing Module Pattern" to hide any implementation and expose just what is needed. The pageScript
variable is the result of an anonymous self-executing function returning an object that exposes just toggleLayout()
, all other variables and functions are hidden within the self-executing function.
This JS code calls the layout()
function when not yet done, and reloads the page (to restore the previous layout) when already done.
It shows my first naive attempt to save the checkbox values. But this does not work, because after location.reload()
the running JavaScript and its execution context (the window
object) have been removed from the browser's memory!
So I needed some other approach to save and restore my checkboxes. The only way to save and restore page settings across a page reload are URL parameters. Because my page is a simple one that is not part of some web application (which already defines URL parameters) this is easy. I have control over the URL to be loaded, and can edit it in any way.
The URL of the currently loaded browser page is in JS variable location.href, which is a read/write variable, meaning that an assignment to location.href will load the assigned URL.
This is needed when saving and restoring values via URL parameters. URL (Unified Resource Locator) and URI (Unified Resource Identifier) have been specified in so-called RFC ("Request For Comment"), see various sources on the internet for that. URL parsing is not "trivial".
Fortunately any web browser provides URL parsing in "a
" elements (hyperlinks).
var parseUrl = function(url) {
if ( ! url )
return location;
var hyperLink = document.createElement("a");
hyperLink.href = url; // triggers URL parsing
return hyperLink;
};
This gives me access to the rough parts of an URL, which are the following for an example like
parseUrl("
http://host:8080/path1/path2/servlet?one=1&two=2#somehash")
hyperLink.
hash = #somehashhyperLink.
host = host:8080hyperLink.
hostname = hosthyperLink.
href = http://host:8080/path1/path2/servlet?one=1&two=2#somehashhyperLink.
pathname = /path1/path2/servlethyperLink.
port = 8080hyperLink.
protocol = http:hyperLink.
search = ?one=1&two=2But as URL parameters are bundled with a starting "?" and "&" separators, like in ?one=1&two=2
, I needed more functionality to access the values of such parameters.
Furthermore care has to be taken to encode and decode their values, because not any character is allowed in an URL (see RFC specification).
The URL parameters are packed in the hyperLink.search
variable. Here are some JS functions to manage URL parameters in relation with the parseUrl()
function above.
They do not depend on jQuery (nice to copy & paste :-).
1 | /** |
This should be everything needed to reload a page and preserve input values. The functions, their parameters and return values are decribed in heading comments.
Notes:The replacing of every '+' by a space is to overcome some older browser's URL space treatment.
You find documentation about regular expressions on the internet.
In short, theRegExp("[?&]"+name+"=([^&]*)")
readsStarting with '?' or '&', then the parameter name, then '=', then zero-to-n characters that must not be '&'.The parentheses around([^&]*)
are to tell the RegExp that the value of the parameter behind the '=' should be returned in the result array.The built-in JS functions
encodeURIComponent()
anddecodeURIComponent()
have been used to pack and unpack any possible value that is stored into an URL parameter.
Following shows how my in-page script
looked like after integrating these functions.
Here I use jQuery again to access the checkboxes to preserve.
1 | <script type="text/javascript"> |
You find the URL-managing functions and an example application on my demo page.
For a more demanding JS library to handle URLs and URL-parameters you might want to have a look on URI.js.
ɔ⃝ Fritz Ritzberger, 2014-12-25