简体   繁体   English

有没有办法在网络浏览器中一次删除所有事件侦听器?

[英]Is there a way to remove all event listeners at once in a web browser?

Simple curiosity here. 简单的好奇心在这里。 Nothing too serious. 没什么太严重的。

Building a new app. 建立一个新的应用程序。 The client side's config file should offer a terminateOnError parameter. 客户端的配置文件应提供terminateOnError参数。 If set to true the whole world should stop after an error is logged. 如果设置为true,则在记录错误后,整个世界都将停止。

Getting out of my showTerminationScreen function without executing any subsequent code should be pretty easy : simply throw an unhandled exception. 退出showTerminationScreen函数而不执行任何后续代码应该很容易:只需抛出未处理的异常。

However the app may (will...) have ongoing asynchronous events, some of which I don't control (I own like half of the app ; the other half is more or less closed source to me) : timers, http request awaiting to come back etc. 但是该应用程序可能(将...)具有正在进行的异步事件,其中一些我不控制(我拥有该应用程序的一半;另一半对我来说或多或少是封闭源):计时器,正在等待http请求回来等等

Would there be a simple way to tell the browser to stop listening to anything it had been tracking so far ? 有没有一种简单的方法可以告诉浏览器停止收听到目前为止一直跟踪的内容?

You have two basic solutions to this problem: 您有两个基本解决方案来解决此问题:

  1. Use event delegation so you only have one element on the whole page that has event handlers attached. 使用事件委托,因此整个页面上只有一个附加了事件处理程序的元素。

  2. Make sure each piece of JavaScript functionality is modular and can detach event handlers on a whim, and then reattach them at a later time. 确保每个JavaScript功能都是模块化的,并且可以一时兴起地分离事件处理程序,然后在以后重新附加它们。

Event Delegation 活动委托

Just searching GitHub gives you oodles of options for event delegation. 只是搜索GitHub即可为您提供大量的事件委托选项。 Some require jQuery. 有些需要jQuery。 Some don't. 有些没有。

<self-promotion type="shameless">

I created Oxydizr , which is an event delegation library that doesn't require jQuery, but does require object oriented JavaScript. 我创建了Oxydizr ,这是一个事件委托库,不需要jQuery,但是需要面向对象的JavaScript。 It utilizes HTML5 attributes to define which user actions lead to method calls in JavaScript. 它利用HTML5属性来定义哪些用户操作导致JavaScript中的方法调用。

</self-promotion>

Writing Module JavaScript Code 编写模块JavaScript代码

You may need to refactor your JavaScript so that it can be initialized and uninitialized: 您可能需要重构JavaScript,以便可以对其进行初始化和未初始化:

Sample JavaScript: 示例JavaScript:

function Foo(element) {
    this.element = typeof element === "string"
        ? document.getElementById(element)
        : element;
    this.handleDeleteItem = this.handleDeleteItem.bind(this);
    this.init();
}

Foo.prototype = {

    constructor: Foo,

    init: function() {
        var items = this.element.querySelectorAll("button.delete-item"),
            length = items.length, i = 0;

        for (i; i < length; i++) {
            items[i].addEventListener("click", this.handleDeleteItem, false);
        }
    },

    uninit: function() {
        var items = this.element.querySelectorAll("button.delete-item"),
            length = items.length, i = 0;

        for (i; i < length; i++) {
            items[i].removeEventListener("click", this.handleDeleteItem, false);
        }
    },

    handleDeleteItem: function(event) {
        event.preventDefault();
        // ... do other stuff
    }

};

The HTML: HTML:

<ul id="foo">
    <li>Item 1
        <button class="delete-item">X</button>
    </li>
    <li>Item 2
        <button class="delete-item">X</button>
    </li>
</ul>

And the JavaScript to kick things off: JavaScript开始了:

var foo = new Foo("items");

When an error occurs: 发生错误时:

foo.uninit();

And if you can resume use of the page: 如果您可以继续使用该页面:

foo.init();

For timers and Ajax requests, you'll need to keep track of all of those objects and clear the timers, and cancel the AJAX requests manually. 对于计时器和Ajax请求,您需要跟踪所有这些对象并清除计时器,并手动取消AJAX请求。

Edit #1 编辑#1

Since large portions of the application are essentially a black box, this becomes an architecture and communication issue. 由于应用程序的大部分实际上是一个黑匣子,因此这成为体系结构和通信问题。 If the "dark half" is something that your company controls, you would need to bring this up with that team and work on a solution. 如果“黑暗的一半”是您公司控制的东西,则需要与该团队合作并制定解决方案。 If they use a JavaScript framework, ask them which framework they use and start learning that framework. 如果他们使用JavaScript框架,请询问他们使用哪个框架,然后开始学习该框架。 You might dig in to the lower level API of the framework and discover a solution. 您可以深入研究框架的较低级别的API并找到解决方案。 The art of Reverse Engineering is your friend in this scenario. 在这种情况下,逆向工程是您的朋友。

not 100% sure but I think that 不确定100%,但我认为

$("*").unbind(); $( “*”)解除绑定()。

should works. 应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM