Cancel onbeforeunload event handler?

To add:

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

To remove:

window.removeEventListener("beforeunload", f_beforeunload);

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