'beforeunload' Chrome Issue

前端 未结 6 1941
不思量自难忘°
不思量自难忘° 2021-02-07 08:40

I have this simple piece of code -

$(window).bind(\'beforeunload\', function(){
    alert(\"Good Bye\")
});

Works great with Firefox, IE8 but

6条回答
  •  滥情空心
    2021-02-07 09:04

    Try this:

    function LogTime(){
    
    jQuery.ajax({
      type: "POST",
      url: "log.php",
      data: "",
      cache: false,
      success: function(response){
    
      }
    });
    

    }

     $(window).bind('beforeunload', function(){
         LogTime();
         return "You're leaving?";
     });
    

    It seems that as long as you return a string for this event at the end of function body, you can run code before that string.

提交回复
热议问题