Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何停止窗口卸载?_Javascript - Fatal编程技术网

Javascript 如何停止窗口卸载?

Javascript 如何停止窗口卸载?,javascript,Javascript,当用户单击另一个链接或刷新页面时,窗口将卸载,如何防止/确认它?我认为onbeforeunload事件处理程序将是您需要的 请尝试以下操作: window.onbeforeunload = confirmExit; function confirmExit() { return "You have attempted to leave this page. If you have made any changes to the fields without clicking

当用户单击另一个链接或刷新页面时,窗口将卸载,如何防止/确认它?

我认为onbeforeunload事件处理程序将是您需要的

请尝试以下操作:

  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
  }
从这里


window.onbeforeunload=function(){return'返回的字符串在这里';}

请看下面的卸载按钮。

您能为这个答案添加一些内容吗?假设链接不存在,这有多有用?@Jonathan Sampson-很好。下次我会这么做的。我会更新这个答案,但被接受的答案基本上是一样的。注意这个功能。除非你阻止人们意外地离开可编辑的上下文,否则人们会讨厌你。
<script type="text/javascript">
     window.onbeforeunload = function(){ return 'Your Returned String Goes Here';}
</script>