Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get all event-listener bindings in Javascript?

I am searching for a way where I can list all event-listener bindings that are made with JS (or other scripts as well) on a website. Mainly I want to find out double bindings (for debug reason), but I guess there are other issues for it as well.

Brilliant would be a plugin for the browser, where you can see on the website which elements have which kinds of eventlisteners bound. You know, some visualization of the event-listeners...

like image 564
helle Avatar asked Mar 14 '11 09:03

helle


3 Answers

Visual Event (http://www.sprymedia.co.uk/article/Visual+Event+2) is very helpful. Go to this page and just drag the "Visual Event" link into your bookmark bar. When you want to check a page, just click it, wait a second, and the events for each element will be overlaid on the page.

like image 120
Luke Avatar answered Oct 27 '22 00:10

Luke


There's only one type of event declaration that you get it, I don't know if this will help you:

// Can't get
myDiv.attachEvent ("onclick", function () {alert (1)});

// Can't get
myDiv.addEventListener ("click", function () {alert (1)}, false);

// Can't get
<div onclick = "alert (1)"></div>

// Can get
myDiv.onclick = function () {alert (1)}

You may look this answer too. Anyway I made a function for you:

<!DOCTYPE html>

<html>
    <head>
        <script>
            function getAllEvents () {
                var all = document.getElementsByTagName ("*");
                var _return = "";

                for (var i = 0; i < all.length; i ++) {
                    for (var ii in all[i]) {
                        if (typeof all[i][ii] === "function" && /^on.+/.test (ii)) { // Unreliable
                            _return += all[i].nodeName + " -> " + ii + "\n";
                        }
                    }
                }

                return _return;
            }

            document.addEventListener ("DOMContentLoaded", function () {
                var div = this.getElementsByTagName ("div")[0];

                div.onclick = function () {
                    alert (1);
                }

                div.onmouseout = function () {
                    alert (2);
                }

                alert (getAllEvents ());
            }, false);
        </script>

        <style>
            div {
                border: 1px solid black;
                padding: 10px;
            }
        </style>
    </head>

    <body>
        <div></div>
    </body>
</html>
like image 27
Caio Avatar answered Oct 27 '22 00:10

Caio


I just wrote a script that lets you achieve this. It gives you two global functions: hasEvent(Node elm, String event) and getEvents(Node elm) which you can utilize. Be aware that it modifies the EventTarget prototype method add/RemoveEventListener, and does not work for events added through HTML markup or javascript syntax of elm.on_event = ..., works only for add/RemoveEventListener.

More info at GitHub

Live Demo

Script:

var hasEvent,getEvents;!function(){function b(a,b,c){c?a.dataset.events+=","+b:a.dataset.events=a.dataset.events.replace(new RegExp(b),"")}function c(a,c){var d=EventTarget.prototype[a+"EventListener"];return function(a,e,f,g,h){this.dataset.events||(this.dataset.events="");var i=hasEvent(this,a);return c&&i||!c&&!i?(h&&h(),!1):(d.call(this,a,e,f),b(this,a,c),g&&g(),!0)}}hasEvent=function(a,b){var c=a.dataset.events;return c?new RegExp(b).test(c):!1},getEvents=function(a){return a.dataset.events.replace(/(^,+)|(,+$)/g,"").split(",").filter(function(a){return""!==a})},EventTarget.prototype.addEventListener=c("add",!0),EventTarget.prototype.removeEventListener=c("remove",!1)}();
like image 39
Gaurang Tandon Avatar answered Oct 27 '22 01:10

Gaurang Tandon