How can I disable "Are you sure you want to leave this page" popups in Chrome?
Those messages are implemented by website developers by listening to the onunload
or onbeforeunload
events.
There is a userscript available from about.com that blocks those events.
In order to install this userscript (or other userscripts, for that sake) you need to first install a Chrome extension called TamperMonkey.
Be careful when installing userscripts, they are capable of doing things you might not want. Only install userscripts from trusted sources.
$(window).off('beforeunload.windowReload');
This is worked for me.
Using jQuery
$(window).off('beforeunload'); // tested in IE 11 and Chrome 62
From the jQuery docs
Calling .off() with no arguments removes all handlers attached to the elements. Specific event handlers can be removed on elements by providing combinations of event names, namespaces, selectors, or handler function names.
So in summation the $(window)
gives us a reference to the window object that is wrapped in a jQuery object. This wrapper gives us access to jQuery APIs that are available on the object (such as .off
). Calling .off()
and providing the string beforeunload
will remove any event listeners that were previously listening for the beforeunload
event.
Note: I did play with the vanilla JS approaches I found after some quick research on Google. However, I was not able to get these approaches to work in the allotted time I had to resolve this issue. If someone has a non jQuery method that is still cross browser compatible please comment or post an additional answer. :)