Search code examples
javascriptjquerypopuponbeforeunload

Showing a jQuery popup before browser window closes


I'm using the following js to show a popup before the browser/window closes. But it does not seem to be working.

$(window).bind('beforeunload', function(e) {
    $('#beforeclose').click(); 
});

The popup i'm trying to show is prettyPopin . And the #beforeclose is the id of the anchor with which i'm trying to bind the click event before the window unloads. I have noticed that native js popups work but the jQuery popup does not work.

Is there a way to achieve what i want to? Please do explain to me or refer me to the respective link because i'm not an expert at js.

Thanks in advance.


Solution

  • You cannot safely do that.

    onbeforeunload is designed in a way to avoid completely preventing the user from leaving your page. The only thing you should do is return a string with a message why the user might not want to leave the page. This message is displayed to the user unless he's using firefox.