Javascript onbeforeunload disable for links

I need your help. I am working with JavaScript and I cannot configure how to work with window.onbeforeunload .

here is my simple code:

 window.onbeforeunload = function () { if (!confirm("some message here")) { return "Are you sure?"; } else{ return false; } } 

I just need to work with the code when I close the browser, reload the page and exit the tab menu. I need it to not work on any links when I click the links in my web body or page. Do you have an idea or solution for this?

I hope you can help me with this. I appreciate your kindness.

thanks

+4
source share
1 answer

You can try the following:

 $(document).on('mousedown', 'a[href]', offBeforeUnload) .on('mouseleave', 'a[href]', function () { $(window).on('beforeunload', windowBeforeUnload); }); function offBeforeUnload(event) { $(window).off('beforeunload'); } function windowBeforeUnload() { if (!confirm("some message here")) { return "Are you sure?"; } else{ return false; } } $(window).on('beforeunload', windowBeforeUnload); 

Demo

+5
source

All Articles