Javascript onbeforeunload 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6218468/
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-10-25 19:55:59  来源:igfitidea点击:

Javascript onbeforeunload function

javascriptjavascript-events

提问by t0mcat

Folks I am trying to call this confirmation function on onbeforeunload event but its not getting called. What am I missing here?
I am calling this inside the body tag: <body onbeforeunload="confirmation();">
I also tried: <body onbeforeunload="return confirmation();">

伙计们,我试图在 onbeforeunload 事件上调用这个确认函数,但它没有被调用。我在这里错过了什么?
我在 body 标签中调用它:<body onbeforeunload="confirmation();">
我也尝试过:<body onbeforeunload="return confirmation();">

code:

代码:

  function confirmation(){
  var answer = confirm('You have unsaved changes!!');
   if(answer == true)
    {
     alert('Bye Bye!!!');
    }
    else{
      alert('You are staying');
     return false;
    }
   }

回答by Matt Ball

That's not remotely how onbeforeunloadworks, and the code contains syntax errors.

这不是远程onbeforeunload工作的方式,并且代码包含语法错误。

window.onbeforeunload = function (e)
{
    var e = e || window.event,
        message = 'You have unsaved changes!!';

    // For IE and Firefox prior to version 4
    if (e)
    {
        e.returnValue = message;
    }

    return message;
}

回答by TweeZz

Soooo.. You're trying to ask the user for confirmation when he made changes on your page that will not be saved if he leaves the page. Is that correct?

Soooo .. 当用户在您的页面上进行更改时,您试图要求用户进行确认,如果他离开页面,则不会保存这些更改。那是对的吗?

If so, then that seems to be possible. Try the following on SO. Start to write an answer on this question and then try to close the browser window.

如果是这样,那么这似乎是可能的。在 SO 上尝试以下操作。开始写下这个问题的答案,然后尝试关闭浏览器窗口。

I tested in Chrome 12, FF 4.0.1 and IE 9.
All 3 browsers show a popupwindow. It seems they all contain a custom text. Chrome shows the cleanestmessage, meaning a popup with only the custom message and 2 buttons.
In all 3 browsers trying to close the browser in any normalway triggers a popup that allows the user to choose if he maybe doesn't rather stay on the page and finish what he started. If he does want to leave, he can. (killing the browser process or a computer shutdown might not do what you would like thoug :) ).

我在 Chrome 12、FF 4.0.1 和 IE 9 中进行了测试。
所有 3 个浏览器都显示一个弹出窗口。似乎它们都包含自定义文本。Chrome 显示最干净的消息,这意味着一个只有自定义消息和 2 个按钮的弹出窗口。
在所有 3 种浏览器中,尝试以任何正常方式关闭浏览器都会触发一个弹出窗口,允许用户选择他是否宁愿留在页面上并完成他开始的操作。如果他真的想离开,他可以。(杀死浏览器进程或关闭计算机可能不会做您想做的事情 :) )。

Internet Explorer:
enter image description here

IE浏览器:
enter image description here

Firefox:
enter image description here

火狐:
enter image description here

Chrome:
enter image description here

铬合金:
enter image description here

I would try the suggestion of hooking up a function that returns a string to the window.onbeforeunload event. Try this:

我会尝试连接一个函数的建议,该函数返回一个字符串到 window.onbeforeunload 事件。试试这个

<script>
window.onbeforeunload = function (evt) {
  var message = 'You have started writing or editing a post.';
  if (typeof evt == 'undefined') {
    evt = window.event;
  }
  if (evt) {
    evt.returnValue = message;
  }
  return message;
}
</script>