如何在退出浏览器会话时触发beforeunload事件

时间:2021-09-03 15:03:25

Disclaimer - I should note that my JavaScript is pretty weak.

免责声明 - 我应该注意到我的JavaScript非常弱。

I'm in the process of inserting code into my website so it can cover a scenario when a user exits his browser, he gets prompted with a proverbial 'Are you sure you want to quit?' sort of confirmation messsage.

我正在将代码插入到我的网站中,因此它可以涵盖用户退出浏览器时的情况,他会收到一句谚语“你确定要退出吗?”某种确认消息。

I'm in the process of trying to implement this piece of JavaScript on my website:

我正在尝试在我的网站上实现这个JavaScript:

   <script language="JavaScript" type0"text/javascript">

var foo = true;

window.onbeforeunload = askBeforeExiting;

function askBeforeExiting() {
  if (foo)
    return "Are you sure you want to leave this page?";
}
</script>

The code not only throws a confirmation message when attempts to exit a browser are made. However, it also throws the confirmation when a link to another portion of the site is clicked and other related events.

代码不仅在尝试退出浏览器时抛出确认消息。但是,当点击链接到网站的其他部分和其他相关事件时,它也会抛出确认。

Is there a way to modify this code to just capture the browser closing scenarios only? I get that scenario like killing the browser from the Task Manager or from a powershell command can't be captured. I just want the scenarios in the current user session where they may exit the browser.

有没有办法修改此代码只捕获浏览器关闭方案?我得到的方案就像从任务管理器中杀死浏览器或者无法捕获powershell命令。我只想在当前用户会话中可以退出浏览器的场景。

I've been looking over various samples across the web and through code references and can't seem to track down exactly what I want to do. Is this even possible?

我一直在通过网络和代码参考查看各种样本,似乎无法准确追踪我想要做的事情。这有可能吗?

1 个解决方案

#1


2  

I've done this before where a page has a form, and I only want to ask the user for confirmation when a change has been made to the form. You could do something similar, where you attach a click event to all links, for example, that would set foo to false. Something like:

我已经在页面有表单之前完成了这个,我只想在对表单进行更改时要求用户确认。您可以执行类似的操作,例如,将click事件附加到所有链接,将foo设置为false。就像是:

var links = document.getElementsByTagName('a');
for(var i = 0, l = links.length; i < l; i++) {
    links[i].onclick = function () {
        foo = false;
    }
}

Edit: There are many other actions you might have to account for, depending upon your site, however with the information given, this should be a good start for you as there is no direct way to accomplish what you are after.

编辑:您可能需要考虑许多其他操作,具体取决于您的网站,但是根据您提供的信息,这应该是一个良好的开端,因为没有直接的方法来完成您的目标。

#1


2  

I've done this before where a page has a form, and I only want to ask the user for confirmation when a change has been made to the form. You could do something similar, where you attach a click event to all links, for example, that would set foo to false. Something like:

我已经在页面有表单之前完成了这个,我只想在对表单进行更改时要求用户确认。您可以执行类似的操作,例如,将click事件附加到所有链接,将foo设置为false。就像是:

var links = document.getElementsByTagName('a');
for(var i = 0, l = links.length; i < l; i++) {
    links[i].onclick = function () {
        foo = false;
    }
}

Edit: There are many other actions you might have to account for, depending upon your site, however with the information given, this should be a good start for you as there is no direct way to accomplish what you are after.

编辑:您可能需要考虑许多其他操作,具体取决于您的网站,但是根据您提供的信息,这应该是一个良好的开端,因为没有直接的方法来完成您的目标。