8

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...

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
helle
  • 11,183
  • 9
  • 56
  • 83
  • 2
    there is something like this for jQuery. Its an extention for firebug its called firequery: http://firequery.binaryage.com/ but i dont know if it work also for native javascript events. – meo Mar 14 '11 at 09:44
  • hm ... it's a first step ... probably needs time to get used to it – helle Mar 14 '11 at 12:28

3 Answers3

4

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.

Luke
  • 18,811
  • 16
  • 99
  • 115
2

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)}();
Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
2

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>
Community
  • 1
  • 1
Caio
  • 3,178
  • 6
  • 37
  • 52