简体   繁体   English

当用户单击某些内容时,onbeforeunload会继续弹出一个警报框

[英]onbeforeunload keeps poping up an alert box when user click something

I am created aa page that warns the user when they click on the (close x) button on the window. 我创建了一个页面,当用户单击窗口上的(关闭x)按钮时向用户发出警告。 I duid some reading and discovered that javascript had a function called onbeforeonload which can take of the job I was tryng to achieve. 我DUID一些阅读和发现的JavaScript有一个函数调用onbeforeonload可以采取我tryng实现工作。 I however found at after my implementation that, when a user clicks on anything in my window (example. save and enter) The dialog box reappears. 然而,我在我的实现当我的窗口(例如保存并进入)上的任何一个用户点击该对话框再次出现,此前发现的。 I was wondering how I could only target the specific X button in the window. 我想知道如何仅将窗口中的特定X按钮作为目标。

    window.onbeforeunload = function (evt) {
  var message = 'Do you want to leave?';
  if (typeof evt == 'undefined') {
    evt = window.event;
  }
  if (evt) {
    evt.returnValue = message;
  }
  return message;
} 

现在正在全局调用该函数...此资源可能帮助您实现所需的目标: http : //randomdrake.com/2009/09/23/how-to-use-onbeforeunload-with-form-submit -纽扣/

This is a "working as intended" behavior for IE. 这是IE的“按预期工作”行为。 Anchor tag clicks, regardless of whether they navigate or not, will trigger the onbeforeunload event . 锚标记单击(无论是否导航)都将触发onbeforeunload事件

This is the workaround I used - I am not sure whether it is the best approach or not: 这是我使用的解决方法-我不确定这是否是最佳方法:

document.onmouseup = function () {
    if (window.event.srcElement.tagName === 'A') {
        // turn off your onbeforeunload handler
        ...

        // some small time later, turn it back on
        setTimeout(..., 200);
    }
};

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

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