简体   繁体   English

使用“后退”按钮时重新加载(刷新)上一页

[英]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. Sharepoint Web应用程序中的所有页面都使用表单,并且我们要求在用户到达页面时必须将表单重置为默认值。 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. 我们已经有适当的代码在页面的第一负载上运行(它运行if(!Page.IsPostback) ),该代码用必要的默认值填充表单。

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. 建议的解决方案是使后退按钮强制重新加载上一页的URL。 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. document.referrer并不总是在IE中工作,并且经常返回NULL,而不是前一页的URL。
  • In IE9, the assignment to window.location throws an "Unspecified error". 在IE9中,对window.location的分配将引发“未指定的错误”。
  • 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. 当刷新页面,关闭选项卡或使用书签链接等时,也会触发window.unload这时不应执行以上代码。

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 获取上一页的URL

The easiest way (but unfortunately not fool-proof, damn you early IE) is to set the autocomplete attribute of the form to off . 最简单的方法(但不幸的是,这并非万无一失,该死的是您早期的IE),是将表单的autocomplete属性设置为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. IE的较旧版本以及几乎每个浏览器的某些真正较旧的版本都忽略了这一点,但是几乎每个现代的浏览器都会在每次显示页面时清除表单。

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. 除此以外, HTMLInputElement确实具有defaultValue 属性,该属性包含在HTML源代码中设置的原始值 -您可以在文档准备就绪时循环遍历所有输入元素,并设置input_elm.value = input_elm.defaultValue值,该值应使用input_elm.value = input_elm.defaultValue覆盖自动完成值实际指定。

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. 尽管我不知道SharePoint是否支持此功能。

Adding this code to my HTML works just fine for me: 将此代码添加到我的HTML中对我来说很好:

<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. 但是,尚未证实它可以在所有主要浏览器上使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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