Is there a way to disable the Are you sure you want to leave this page? message on a website? I'm using Chrome.

7 Answers 7


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.


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. :)

  • I was unsuccessful using the accepted answer, and jQuery was already available on the page.
    – wickdninja
    Nov 21, 2017 at 15:31
  • 2
    This would be a better answer if you would explain how to use it, and, ideally, something about how it works. Nov 21, 2017 at 19:19
  • This is SuperUser, not Stack Overflow. Users aren't expected to know how to implement this line of code.
    – Flimm
    Mar 14 at 14:35

This is worked for me.

  • 6
    A bit more detail on your proposed solution would help
    – Dave M
    Aug 9, 2017 at 12:08
  • poor description. typed this in console but no result. don't work for me.
    – Argus
    Jan 30, 2020 at 7:14
  • Downvoted for jQuery-specific solution without context. Mar 29, 2021 at 14:16
  • This is SuperUser, the users are not expected to be programmers and to understand how to implement this line of code.
    – Flimm
    Mar 14 at 14:34

Here's an alternative, manual way to remove beforeunload event listeners:

  1. Right click your web page in Chrome and choose Inspect from the menu, or type Ctrl+Shift+I.
  2. Make sure you are in the Elements tab and that the right side panel is visible, if it isn't make sure the Inspect window is wide enough.
  3. On the right side panel pick the "Event Listeners" tab.
  4. Locate the beforeunload event listener in the list and expand it
  5. Use the "Remove" button for all the event listeners under it.

visual instructions

And a vanilla-JS way that doesn't require jQuery. Thanks to Mike Sraj:

function removeListenersFromElement(element, listenerType){
    const listeners = getEventListeners(element)[listenerType];
    let l = listeners.length;
    for(let i = l-1; i >=0; i--) {
        removeEventListener(listenerType, listeners[i].listener);
removeListenersFromElement(window, "beforeunload");

so to get the command from @wickdninja working, first install Chrome Developer Tools. Then you can open that and you will see a tab for 'Console'. Click the Console tab. Then to enable jQuery type these commands:

var jqry = document.createElement('script');
jqry.src = "https://code.jquery.com/jquery-3.3.1.min.js";

Then type:


after that i am able to open/close my gmail without getting the 'Leave?' prompt. https://developers.google.com/web/tools/chrome-devtools/console/javascript

hope this helps.

  • That solutions don't work. Also I don't understand how to "install Chrome Developer Tools". Chrome Developer Tool is a part of Chrome Browser already. There is no way to install/uninstall it.
    – Argus
    Jan 30, 2020 at 7:13

If you are running in. python + selenium

    def super_get(url):
        driver.execute_script("window.onbeforeunload = function() {};")


    var script = document.createElement( 'script' );
    script.type = 'text/javascript';
    script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js';

  • This is SuperUser, not Stack Overflow. We are trying to help non-programmers.
    – Flimm
    Mar 14 at 14:35

You can disable Javascript on a site-by-site basis. Go to Settings --> Show advanced settings --> Privacy --> Content settings --> Javascript --> Manage exceptions.

  • 4
    Yes, but that will disable more than just the message.
    – BenjiWiebe
    Jul 17, 2014 at 12:41
  • In FIrefox at least, disallowing facebook to run javascript doesn't even accomplish what the OP intends as the page simply keeps reloading the script without executing it.
    – brokkr
    Mar 30, 2015 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy