All the pages in our Sharepoint web application use forms, and we have a requirement that the forms must be reset (to default values) when the user arrives onto a page. We already have code in place that runs on first-load of the page (it runs if(!Page.IsPostback)
) that populates the form with the necessary default values.
However, when the back button is used, this behaviour is broken. The form retains the values that were used when the page was last visited. I believe this is because back-button does not reload the page, but simply brings it back from cache.
The proposed solution was to cause the back-button to force reload the URL of the previous page. This is the code I have:
//$(window).unload(function () { //does not work in Firefox
$(window).bind('beforeunload', function () {
// when the back button is hit, obtain the
// URL of the prev. page (document.referrer)
// and manually navigate to it forcing the page to reload
try {
if (document.referrer) {
window.event.returnValue = false;
window.location = document.referrer + "?q=" + $.now();
}
}
catch (e) {
// unload will also be triggered during a postback,
// at which time an "Access Denied" error will be
// thrown for the usage of window.location. This error
// can be ignored, since this is the right behaviour.
}
});
This code does not work as intended.
- The
document.referrer
does not always work in IE, and returns NULL often, instead of the URL of the previous page. - In IE9, the assignment to
window.location
throws an "Unspecified error". window.unload
is also triggered when the page is refreshed, tab is closed, or a bookmark link is used, etc. at which times the code above should not be executed.
I'm looking for a fool-proof way to:
- Distinguish the back-button press during unload from other unload techniques.
- Obtain the URL to the previous page