4

How can I disable onbeforeunload for links?

var show = true;

function showWindow(){
    if(show){      
        alert('Hi');
        return "Hi Again";
    }
}


$('a').click(function(){

    show = false;

});


window.onbeforeunload = showWindow;

This is what I have, but it still shows when I click on an 'a' element

Button code:

<button type="submit" class="submitBtn"><span>Open Account</span></button>
dzm
  • 22,844
  • 47
  • 146
  • 226

4 Answers4

15

Instead of

show = false;

try

window.onbeforeunload = null;

This will simply unbind the function from the event.

JustcallmeDrago
  • 1,885
  • 1
  • 13
  • 23
  • 1
    Nice, so that works for links - however for button it does not. – dzm Nov 03 '10 at 04:21
  • try adding an alert() as the first line of code in your jquery click event handler. If your "hi" alert shows up before your new alert, that means that the onbeforeunload event fires before your button click handler fires. Is this the case? – JustcallmeDrago Nov 03 '10 at 04:24
  • That's right, it's firing the onbeforeunload before the button click handler. – dzm Nov 03 '10 at 04:25
  • You can add a mouseDown handler to the submit button that unbinds the onbeforeunload event and also binds a mouseup event to the document that rebinds the function to onbeforeunload IF the mouseup target was NOT the button. This way the onbeforeunload will not be fired if both the mousedown and up (===click) were on the button, or else everything will be returned back to normal once the mouse is unclicked over anything else. – JustcallmeDrago Nov 03 '10 at 04:37
  • I'm still trying to figure out how to suppress this Leave/Stay prompt conditionally. In other words, if the form is dirty, allow the prompt to occur, otherwise bypass it. Any ideas on how to achieve this? – PongGod Apr 15 '21 at 19:46
11

I just ran into the same problem and found a very easy solution:

$("a").mousedown(function() {
    $(window).unbind();
});

This will remove the onbeforeunload event just before the click event is triggered.

Ginchen
  • 790
  • 7
  • 20
  • 1
    Not sure why, but this was the only solution that worked for me. I'm working on a custom post type in WordPress, and needed to do some processing when the user hits the WP 'update' button, and then submit the form with JS. I had tried `window.onbeforeunload = null;` but that didn't work. This did. Thanks! – cmpreshn May 13 '15 at 20:53
  • 2
    Because `mousedown` runs before `beforeunload` and `click` runs after it. I would still recommend using JustcallmeDrago solution, while replacing the link `click` event with a `mousedown` – oriadam Jun 30 '15 at 14:08
5

Use following code to unbind the event:

javascript:window.onbeforeunload=function(){null}
qasimzee
  • 640
  • 1
  • 12
  • 30
1

For some reason, using window.onbeforeunload = null did not work for me.

What did the trick instead was adding and removing the listener accordingly:

let onBeforeUnloadListener;

// Mounting
window.addEventListener('beforeunload', onBeforeUnloadListener = ev => {
  ev.preventDefault();
  ev.returnValue = 'Any';
});

// Unmounting
window.removeEventListener('beforeunload', onBeforeUnloadListener);

This idea came to mind due to using getEventListeners(window), which shown the active beforeunload listeners.

Andrei Gătej
  • 11,116
  • 1
  • 14
  • 31