There is a tab in which the page is open, how to make an alert with text appear when a person tries to close a tab?

    1 answer 1

    If jQuery version is higher than 1.8 :

     $(window).bind("beforeunload", function() { return confirm("Do you really want to close?"); }) 

    Below:

     $(window).unload(function() { alert("Bye me!"); }) 

    Try it.

    UPDATE:

     jQuery(window).bind('beforeunload', function(e) { var message = "Why are you leaving?"; e.returnValue = message; return message; }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <a href="http://www.google.com">Exit</a> 

    UPDATE # 2:

     $(window).on('beforeunload', function() { return 'Your own message goes here...'; }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <a href="http://google.com">Exit!</a> 

    • In firefox it does not work for some reason - Anatoly
    • @ Anatoly Which option have you tried? I recommend using the first option. - user192664
    • YES I do here on LAN, I tried both options - Anatoly
    • In Fox, when the link transition works, but when I close the window, it is not working - Anatoly
    • @ Is Anatoly the first one not working at all, or only in firefox? - user192664