最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript How to call a function when we choose Stay on Page in Chrome - Stack Overflow

programmeradmin3浏览0评论

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

When I click the 2. Stay on this page button it must activate my custom function 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>

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

When I click the 2. Stay on this page button it must activate my custom function 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>
Share Improve this question asked May 16, 2012 at 10:13 Me 4UMe 4U 2762 gold badges4 silver badges13 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 7

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

You can use a combination of the onbeforeunload and onunload events, setting a timer in the former and clearing it in the latter:

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 onunload event never fires when the user chooses to stay on the page.

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>
发布评论

评论列表(0)

  1. 暂无评论