Javascript 当我们在 Chrome 中选择留在页面时如何调用函数

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

Javascript How to call a function when we choose Stay on Page in Chrome

javascriptjqueryhtml

提问by Me 4U

Please check my code in Chrome Browser, if you hit refresh you will be prompted with 2 options.

请在 Chrome 浏览器中检查我的代码,如果您点击刷新,您将收到 2 个选项的提示。

  1. Leave This Page and
  2. Stay on This Page
  1. 离开此页面并
  2. 保持此页上

When I click the 2. Stay on this pagebutton it must activate my custom function displayMsg()

当我单击2. 停留在此页面按钮时,它必须激活我的自定义函数displayMsg()

Can any one provide me solution?

任何人都可以为我提供解决方案吗?

<script type="text/javascript">
function displayMsg() {
    alert('my text..');
}
</script>
<script type="text/javascript">
window.onbeforeunload = function(evt) {
  var message = 'Please Stay on this page and we will show you a secret text.';
  if (typeof evt == 'undefined') {
      evt = window.event;
  }       
  if (evt) {
      evt.returnValue = message;
      return message;
  }
  trace(evt);
} 
</script>

采纳答案by M Rostami

use a timer to listening for change variable :

使用计时器来监听变化变量:

var vals=0;
function displayMsg() {
    alert('my text..');
}
window.onbeforeunload = function evens(evt) {
var message = 'Please Stay on this page and we will show you a secret text.';
  if (typeof evt == 'undefined') {
      evt = window.event;
  }       
    timedCount();
    vals++;
  if (evt) {
      evt.returnValue = message ;
      return message ;
  }
  trace(evt);
} 

function timedCount()
{
t=setTimeout("timedCount()",100);
if(vals>0)
{
    displayMsg();
    clearTimeout(t);
}
}

回答by Andy E

You can use a combination of the onbeforeunloadand onunloadevents, setting a timer in the former and clearing it in the latter:

您可以使用onbeforeunloadonunload事件的组合,在前者中设置计时器并在后者中清除它:

var showMsgTimer;

window.onbeforeunload = function(evt) {
    var message = 'Please Stay on this page and we will show you a secret text.';
    showMsgTimer = window.setTimeout(showMessage, 500);

    evt = evt || window.evt;
    evt.returnValue = message;

    return message;
}

window.onunload = function () {
    clearTimeout(showMsgTimer);
}

function showMessage() {
    alert("You've been trolled!");
}

This works because the onunloadevent never fires when the user chooses to stay on the page.

这是有效的,因为onunload当用户选择留在页面上时,事件永远不会触发。

回答by FAISAL

Please use these lines of code , it works properly.

请使用这些代码行,它可以正常工作。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
    </script>
    <script type="text/javascript">
    $(function(){
    $(window).bind('beforeunload', function(){
      return 'Your Data will be lost :(';
     });
    });
    </script>