Cancel onbeforeunload event handler?

前端 未结 4 886
孤独总比滥情好
孤独总比滥情好 2021-01-07 18:41

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

window.onbeforeunload = function()          


        
4条回答
  •  梦谈多话
    2021-01-07 18:45

    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

提交回复
热议问题