Javascript 阻止 Firefox 重新加载确认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12622019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:51:41  来源:igfitidea点击:

Preventing Firefox reload confirmation

javascriptfirefoxgoogle-chromereloadonbeforeunload

提问by Emmi

I'm displaying certain records in an editable table. The user when attempts to reload the table while editing a record a pop up comes warning the record about the unsaved data.

我在可编辑的表格中显示某些记录。用户在编辑记录时尝试重新加载表时,会弹出一个警告记录有关未保存数据的记录。

function cancelProcess()
{
    if(noEditedRecords !=0)//number of edited records in the table
    {
        var processConfirmation = confirm("You've Edited "+ noEditedRecords +" Records. Are You sure to undo the Changes made?");

        if (processConfirmation ==true){
            window.onbeforeunload = null;
            window.location.reload();
        }
    }
}

When he clicks OK to reload the page, Firefox prompts as

当他点击 OK 重新加载页面时,Firefox 提示为

To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier.

要显示此页面,Firefox 必须发送信息来重复之前执行的任何操作(例如搜索或订单确认)。

And when opening the same page in Chrome, no such prompt appears.

而在 Chrome 中打开同一个页面时,也不会出现这样的提示。

I tried to avoid this by setting window.onbeforeunload = null;, but still the prompt window appears there.

我试图通过设置来避免这种情况window.onbeforeunload = null;,但提示窗口仍然出现在那里。

Also I tried by changing Firefox configuration:

我也尝试通过更改 Firefox 配置:

browser.sessionstore.postdata

browser.sessionstore.postdata

Changed 0 to 1as suggested in Mozilla support page.

按照 Mozilla 支持页面中的建议将 0 更改为 1

But nothing worked.. How do I prevent the prompt?

但没有任何效果..如何防止出现提示?

回答by Ricardo Huertas

Using

使用

window.location=window.location;

Instead of

代替

location.reload();

work for me.

为我工作。

回答by Mikeys4u

I solved this, and sent data as only GETinstead of POST,

我解决了这个问题,并仅以GET而不是POST 的方式发送数据,

It my not suit all needs but it works....

它不适合所有需求,但它有效......

I was also using location.reload();

我也在使用location.reload();

回答by Seva Bityukov

window.opener.location.href = window.opener.location;

I've found the decision here

我在这里找到了决定

(Tested on Firefox 32 and Chrome 38 successfully.)

(在 Firefox 32 和 Chrome 38 上测试成功。)

回答by Alexandru Cosoi

The behavior is correct. According to w3schools reload has a parameter forceGetthat is default false, and as a result if you have a POST submit, the the browser will try to resend that POST data, and as such, it needs your confirmation. Firefox does this right, and google chrome behaves like it has a default of true for forceGet

行为是正确的。根据 w3schools reload 有一个参数forceGet,默认为 false,因此如果您有 POST 提交,浏览器将尝试重新发送该 POST 数据,因此,它需要您的确认。Firefox 这样做是正确的,而 google chrome 的行为就像它的forceGet的默认值为 true

http://www.w3schools.com/jsref/met_loc_reload.asp

http://www.w3schools.com/jsref/met_loc_reload.asp

while window.location=window.locationor window.location=window.location.href(or any other variation of this) works most of the time, when you are using an anchor url like http://www.google.com#testwill not be refreshed with this method. The browser will do the exact same thing as it will do when you are already on google.com and you change the url by adding #test. The browser will just try to locate the anchor tag in the page that has the name test, and scroll down to it, without refreshing your browser.

window.location=window.locationwindow.location=window.location.href(或此的任何其他变体)大部分时间都有效,当您使用像http://www.google.com#test这样的锚点 url 时,此方法不会刷新。当您已经在 google.com 上并通过添加 #test 更改 url 时,浏览器将执行完全相同的操作。浏览器只会尝试在具有名称 test 的页面中找到锚标记,然后向下滚动到它,而不会刷新浏览器。

The solution that works in this case as well as any other case I encountered would be window.location.reload(true);

在这种情况下以及我遇到的任何其他情况下都有效的解决方案是 window.location.reload(true);

Hope this helps,

希望这可以帮助,

Alexandru Cosoi

亚历山德鲁·科索伊

回答by Boris Zbarsky

The way to avoid that prompt is to not create a situation where a user is trying to reload a POST result page.

避免该提示的方法是不要造成用户尝试重新加载 POST 结果页面的情况。

回答by CookieMonster

Try this.

尝试这个。

setTimeout (function () {
    window.location.reload();
},0);