简体   繁体   中英

Reload (fresh-load) previous page when back button is used

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:

  1. Distinguish the back-button press during unload from other unload techniques.
  2. Obtain the URL to the previous page

The easiest way (but unfortunately not fool-proof, damn you early IE) is to set the autocomplete attribute of the form to off . This usually prevents past values from being retained, either when refreshed or returned to via the history. Older versions of IE, and some really-old versions of nearly every browser ignore this, but nearly every modern browser will clear the form every time the page is displayed.

Barring that, the HTMLInputElement does have a defaultValue property that contains the original value set in the HTML source - you could loop through all input elements at document ready and set input_elm.value = input_elm.defaultValue which should overwrite the autocomplete value with whatever your source actually specifies.

You could set a variable before navigating to the next page or unloading. Then you can simply check this variable when the script is reloaded from cash and act accordingly. Possibly you can use anti-forgery tokens for this too, this way you already have a way of checking if the form is legit. Although I don't know if SharePoint supports this.

Adding this code to my HTML works just fine for me:

<input id="isOld" type="hidden" />
<script>
    setTimeout(function () {
        var el = document.getElementById('alwaysFetch');
        el.value = el.value ? location.reload() : true;
    }, 0);
</script>

It assigns a value to the hidden input that will remain after the back button is clicked in which case the page is force refreshed.

However it's yet to be confirmed that it works on all major browsers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM