31

I have an onbeforeunload event handler attached to the page which executes every time the page reloads / gets redirected.

window.onbeforeunload = function() { console.log("do something");} 

I do not want this script to run during my tests. I want to disable it in my own test environment.

Is there some way to unbind this onbeforeunload event? I am using JavaScript, with jQuery as the framework, so an answer in jQuery would be good.

4 Answers 4

56

In javascript functions can be overwritten. You can simply assign it a new purpose:

window.onbeforeunload = function () { // blank function do nothing } 

This will completely overwrite the existing version of window.onbeforeunload.

Note: Why don't you simply remove the line of code that sets this function in the first place? Or if you can't, you will have to set this blank function after it is has been defined, just to make sure it is not overridden again

Sign up to request clarification or add additional context in comments.

4 Comments

This worked perfectly inside our AJAX functions that hit this same problem. +1
or window.onbeforeunload = null;
This is not overriding once previous method called. If previous method called once, even after assigning new one not helping in my case. It still goes to previous method.
You can only override it before it is called.
7

To add:

function f_beforeunload(e) {console.log("do something");} window.addEventListener("beforeunload", f_beforeunload); 

To remove:

window.removeEventListener("beforeunload", f_beforeunload); 

Comments

4

Not immediately related to this question, but may help someone nevertheless. This is what I use in Angular 1.x and TypeScript:

this.$window.onbeforeunload = () => undefined; 

4 Comments

Maybe because this requires angular and TypeScript to do something that can be done in a single line of code without it.
Perhaps, but I explicitly stated that while slightly unrelated, someone who may be using Angular may find use in this. TypeScript in this code is irrelevant - it just happened to be what's part of my setup. I can understand not up-voting but down-voting is a bit petty. Then again, maybe I'm getting my jimmies in a twist here.
FYI: this method causes IE11 to show the confirmation message with the message "null". Replacing null with undefined fixes it.
Does it have to be a function? The default value of onbeforeload is null, so can't you just set it to null? this.$window.onbeforeunload = null;
0

jQuery ≥ 1.7

With jQuery 1.7 onward the event API has been updated, .bind()/.unbind() are still available for backwards compatibility, but the preferred method is using the on()/off() functions. The below would now be,

$('#myimage').click(function() { return false; }); // Adds another click event $('#myimage').off('click'); $('#myimage').on('click.mynamespace', function() { /* Do stuff */ }); $('#myimage').off('click.mynamespace'); $( window ).on( "unload", handler ) 

As the .unload() method is just a shorthand for .on( "unload", handler ), detaching is possible using .off( "unload" )

$( window ).off( "unload", handler ) 

3 Comments

What does this have to do with onbeforeunload?
As the .unload() method is just a shorthand for .on( "unload", handler ), detaching is possible using .off( "unload" )
@murthynaikak unload != onbeforeunload hence the onbefore

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.