$(window).unload is not firing

后端 未结 6 1783
失恋的感觉
失恋的感觉 2020-11-28 15:23

I want to execute an action method when the user is abandoning a particular page using jQuery.

The page has the following code:

    

        
相关标签:
6条回答
  • 2020-11-28 15:23

    as bhelm said beforeunload works for me as well.
    return false on this event will invoke the browser default

    are you sure you want to leave this page?


    $(window).on('beforeunload', function ()
        {
            return false;
        });
    
    0 讨论(0)
  • 2020-11-28 15:23

    In many projects of mine, the mentioned methods here are instable. The only thing that works for me is to bind the event as original attribute on the body element.

    <body onunload="my_function_unload()">
    

    jQuery method:

    $('body').attr('onunload', 'my_function_unload()');
    

    From an iframe:

    <body onunload="window.parent.my_function_unload()">
    

    jQuery method:

    $('<iframe />').load(function(){
        $body = $(this).contents().find('body');
        $body.attr('onunload', 'window.parent.my_function_unload()');
    }
    

    Also, important, no arguments in the attribute, and the function must be in the global window scope, otherwise nothing happens.

    For example, common mistake If your my_function_unload() are wrapped inside a ;( function( $ ) {... OR $(document).ready(function(){... AS my_function_unload() must be outside that private scope. And dont forget to use jQuery instead of $ prefix then.

    0 讨论(0)
  • 2020-11-28 15:27

    jquery .on('unload',..); was not reliable working for me. i switched over to use beforeunload. just make sure you are not returning anything, or the user will get a "are you sure to leave the page"-popup.

    <script type='text/javascript'>
        $(window).on('beforeunload', function(){
             console.log("beforeUnload event!");
         });
    </script>
    
    0 讨论(0)
  • 2020-11-28 15:37

    Actually some browsers such as Google Chrome might block if you attempt to alert in a window unload. As a user I like this feature. Alerting everytime you try to navigate away from a page sucks:

    enter image description here

    Replace the alert with a console.log or something else less intrusive to the user and the event will be happily called.

    You might also want to checkout the onbeforeunload event.

    0 讨论(0)
  • 2020-11-28 15:40

    if you want to alert user that you leaving this page then if this

    /* $(window).unload(function()
    {
    	//alert("you leaving this page");
    	console.log("you leaving this page");
    }); */

    function not work for you then replace your code with on("beforeunload",function) like this

    $(window).on("beforeunload", function()
    {
    	alert("you leaving this page");
    	console.log("you leaving this page");
    });

    this work for me ! you can see the output in console log you leaving this page

    0 讨论(0)
  • 2020-11-28 15:48
    window.onbeforeunload=navigationError;
    
    var dont_confirm_leave = 0; var leave_message = 'You sure you want to leave?';
    
    function navigationError(e) 
      {
        if(dont_confirm_leave!==1)
        {
          if(!e) e = window.event;
          //e.cancelBubble is supported by IE - this will kill the bubbling process.
          e.cancelBubble = true;
          e.returnValue = leave_message;
          //e.stopPropagation works in Firefox.
          if (e.stopPropagation) 
          {
            e.stopPropagation();
            e.preventDefault();
          }
    
          //return works for Chrome and Safari
          return leave_message;
        }
      }
    
    0 讨论(0)
提交回复
热议问题