如何使用 jquery 或 javascript 检查页面是否重新加载或刷新?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10400182/
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
How to check page is reloading or refreshing using jquery or javascript?
提问by user957178
I have to do some kind of operation on the page refresh or reload. that is when I hit next page or Filter or refresh on the grid. I need to show some confirmation box over this Events.
我必须对页面刷新或重新加载进行某种操作。那是当我点击下一页或过滤或刷新网格时。我需要在此事件上显示一些确认框。
is there any event which can tell you page is doing filer? refresh or paging? using javascript?
是否有任何事件可以告诉您页面正在执行文件管理器?刷新还是分页?使用javascript?
Thanks
谢谢
回答by AlienWebguy
If it is refreshing, window.onunload
will fire.
如果是清爽,window.onunload
就会火。
// From MDN
window.onunload = unloadPage;
function unloadPage()
{
alert("unload event detected!");
}
https://developer.mozilla.org/en/DOM/window.onunload
https://developer.mozilla.org/en/DOM/window.onunload
If you just want a confirmation box to allow them to stay, use this:
如果您只想要一个确认框以允许他们留下,请使用以下命令:
window.onbeforeunload = function() {
return "Are you sure you want to navigate away?";
}
回答by pala?н
You can create a hidden field and set its value on first page load. When the page is loaded again, you can check the hidden field. If it's empty then the page is loaded for the first time, else it's refreshed. Some thing like this:
您可以创建一个隐藏字段并在第一页加载时设置其值。当页面再次加载时,您可以检查隐藏字段。如果它是空的,那么页面是第一次加载,否则它会被刷新。像这样的事情:
HTML
HTML
<body onLoad="CheckPageLoad();">
<input type="hidden" name="visit" id="visit" value="" />
</body>
JS
JS
function CheckPageLoad() {
if (document.getElementById("visit").value == "") {
// This is a fresh page load
document.getElementById("visit").value = "1";
}
else {
// This is a page refresh
}
}?
回答by Jason Morello
There are some clarification notes on wrestling with this I think are critical.
我认为有一些关于与此搏斗的澄清说明很重要。
First, the refresh/hidden field system works on the beginning of the new page copy and after, not on leaving the first page copy.
首先,刷新/隐藏字段系统适用于新页面副本的开头和之后,而不是离开第一页副本。
From my research of this method and a few others, there is no way, primarily due to privacy standards, to detect a refresh of a page during unload or earlier. only after the load of the new page and later.
根据我对这种方法和其他一些方法的研究,主要是由于隐私标准,无法在卸载或更早期间检测页面刷新。只有在加载新页面之后。
I had a similar issue request, but basically it was terminate session on exit of page, and while looking through that, found that a browser treats a reload/refresh as two distinct pieces:
我有一个类似的问题请求,但基本上它是在页面退出时终止会话,并且在查看时发现浏览器将重新加载/刷新视为两个不同的部分:
- close the current window (fires onbeforeunload and onunload js events).
- request the page as if you never had it. Session on server of course has no issue, but no querystring changes/added values to the page's last used url.
- 关闭当前窗口(触发 onbeforeunload 和 onunload js 事件)。
- 请求页面,就像您从未拥有过一样。服务器上的会话当然没有问题,但没有查询字符串更改/向页面上次使用的 url 添加值。
These happen in just that order as well. Only a custom or non standard browser will behave differently.
这些也只是按照这个顺序发生。只有自定义或非标准浏览器的行为会有所不同。
回答by Aurora
$(function () {
if (performance.navigation.type == 1) {
yourFunction();
}
});
More about PerformanceNavigation
object returned by performance.navigation
有关PerformanceNavigation
返回的对象的更多信息performance.navigation