使用 Internet Explorer 时是否有办法阻止 onbeforeunload 事件触发

发布于 2024-08-31 09:14:46 字数 286 浏览 11 评论 0原文

我有一个函数,应该在用户关闭浏览器时触发,并且我已将代码放入 window.onbeforeunload 函数中。

问题是,每次我在 Internet Explorer 中重新加载页面时,onbeforeunload 事件也会触发,这是一个问题,因为我只希望它仅在用户关闭或导航离开当前页面时触发但不在页面刷新/重新加载上。

因此,我不确定 onbeforeunload 是否会在页面刷新/重新加载时触发,如果是这样,那么还有其他方法可以解决它吗?

I have a function that is suppose to trigger when user closes their browser and I have put the code in the window.onbeforeunload function.

The thing is every time if I reloads the page in Internet Explorer, the onbeforeunload event will also trigger which is a problem because I only wants it to trigger only when the user closes or navigates away from the current page but not on a page refresh/reload.

Therefore I'm not sure if onbeforeunload is intended to trigger even on a page refresh/reload and if it is intended to, then is there another way to work round it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆头 2024-09-07 09:14:46

由于您实际上依赖于 javascript,因此您可能需要研究解决为什么他们必须刷新页面的问题。您可以使用 XMLHttprequest 刷新它们的内容,以便仅在需要时调用所需的 onbeforeunload 函数。

Since you are relying on javascript as it is, you may want to look into resolving the issue as to why they have to refresh the page. You can use XMLHttprequest to refresh the content for them so that the desired onbeforeunload function is only called when it needs to be.

云裳 2024-09-07 09:14:46

没有聪明的方法来解决这个问题。页面上的任何卸载操作都会触发 unloadbeforeunload 事件,并且无法区分刷新和导航之间的区别。

您可以尝试一些方法,但没有 100% 的方法。例如,捕获 F5Ctrl+R 键将识别刷新,您可以为此取消设置 onbeforeunload 处理程序,但对于单击工具栏上的刷新/重新加载按钮的用户来说,它不起作用。您可以将事件处理程序附加到 元素或任何

onsubmit 上的所有点击,但这不会'对于从您的页面的地址栏中输入新地址的用户没有帮助。

There's no smart way to work around it. Any unloading action on the page will fire the unload and beforeunload events and there's no way to tell the difference between a refresh and a navigation.

You could attempt a couple of things, but there's no 100% method. For instance, capturing the F5 or Ctrl+R keys would identify a refresh, for which you could unset the onbeforeunload handler, but it would not work for users who click the refresh/reload button on their toolbar. You could attach an event handler to all clicks on an <a> element or any <form> onsubmits, but this wouldn't help for users who type a new address into the address bar from your page.

柳若烟 2024-09-07 09:14:46

即使使用XMLHttprequest刷新,IE也有问题。您必须调用包含 XMLHttprequest 的 javascript 函数,例如,

<a href="javascript:void(0)" onclick="addContent();">click to add content</a> 

将在 IE 上触发 onbeforeunload 事件,但不会在 Safari 或 Firefox 上触发。

在某些情况下有效的一种解决方案是有条件地处理事件,当您想要加载内容时将其关闭,然后重新打开

var guard = true; 
function myOnbeforeunloadHandler() 
{   
    if(guard)
    {
        return "you are about to leave the page and lose data";
    } 
}

function addContent()
{
    getElementById("myDiv").html = "<p>some content</p>";
    guard = true;
}

<a href="javascript:void(0) onclick="guard=false;addContent();> click to add content</a>

Even if you use XMLHttprequest to refresh, IE has a problem. You have to call the javascript function that contains the XMLHttprequest, for example,

<a href="javascript:void(0)" onclick="addContent();">click to add content</a> 

will trigger an onbeforeunload event on IE, but not on Safari or Firefox.

One solution that'll work in some situations is to handle the event conditionally, turning it off when you want to load content then turning it back on

var guard = true; 
function myOnbeforeunloadHandler() 
{   
    if(guard)
    {
        return "you are about to leave the page and lose data";
    } 
}

function addContent()
{
    getElementById("myDiv").html = "<p>some content</p>";
    guard = true;
}

<a href="javascript:void(0) onclick="guard=false;addContent();> click to add content</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文