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

后端 未结 3 872
隐瞒了意图╮
隐瞒了意图╮ 2021-02-13 18:35

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

  1. Leave This Page and
  2. Stay on This Page
3条回答
  •  一生所求
    2021-02-13 18:44

    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);
    }
    }
    

提交回复
热议问题