取消onbeforeunload事件处理程序? [英] Cancel onbeforeunload event handler?

查看:106
本文介绍了取消onbeforeunload事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个onbeforeunload事件处理程序附加到页面,每次页面重新加载/重定向时都会执行。

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.

有没有办法取消绑定这个onbeforeunload事件?我使用JavaScript,以jQuery作为框架,所以jQuery中的答案会很好。

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.

推荐答案

在javascript函数中可以覆盖。你可以简单地为它指定一个新目的:

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

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

这将完全覆盖现有版本的窗口.onbeforeunload

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

这篇关于取消onbeforeunload事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆