Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detach event in IE 6 7 8 9 using JavaScript

This is a partial code, not the full version.

I have a highlighter that highlights a specific html element when the mouse hovers.

I also have a click event and listener.

My problem is : the highlighter event/listener does not detach when using Internet Explorer v6 v7 v8 v9

What am I doing wrong?

This is how I attach the event and start the event listener:

if (document.body.addEventListener) {
                        //alert(11);
                        document.body.addEventListener('mousemove', handler, false);
                    } else if (document.body.attachEvent) {
                        //alert(12);
                        var ff=function(e) {
                            return handler(e || window.event);
                        };
                        //alert(ff);
                        document.body.attachEvent('onmousemove', ff);
                    } else {
                        //alert(13);
                        document.body.onmousemove = handler;
                    }

This is how I stop the onmousemove/mouse event/listener :

if (document.body.removeEventListener) {
                    document.body.removeEventListener('mousemove', handler, false);
                } else if (document.body.detachEvent) {
                    document.body.detachEvent('onmousemove', function(e) {
                        return handler(e || window.event);
                    });
                } else {
                    document.body.removeAttribute("onmousemove");
                }

This is how I stop the onclick/click event/listener:

if (document.body.removeEventListener) {
                    document.body.removeEventListener('click', ClosetAffairHighlighter.highlightClick, false);
                } else if (document.body.detachEvent) {
                    document.body.detachEvent('onclick', ClosetAffairHighlighter.highlightClick);
                } else {
                    document.body.removeAttribute("onclick");
                }
like image 509
Ionut Flavius Pogacian Avatar asked Oct 18 '12 07:10

Ionut Flavius Pogacian


People also ask

How do you remove an event from an element?

The removeEventListener() is an inbuilt function in JavaScript which removes an event handler from an element for a attached event. for example, if a button is disabled after one click you can use removeEventListener() to remove a click event listener.

Do I need to remove event listeners in JS?

TLDR; Always remove event listeners when you don't plan on using them any longer.


1 Answers

base on this article, a cross-browser event handler can be :

var EventUtil = {
    addHandler: function(element, type, handler) {
        if (element.addEventListener) {
            element.addEventListener(type, handler, false);
        } else if (element.attachEvent) {
            element.attachEvent("on" + type, handler);
        } else {
            element["on" + type] = handler;
        }
    },
    removeHandler: function(element, type, handler) {
        if (element.removeEventListener) {
            element.removeEventListener(type, handler, false);
        } else if (element.detachEvent) {
            element.detachEvent("on" + type, handler);
        } else {
            element["on" + type] = null;
        }
    }
};
like image 180
Chris Li Avatar answered Oct 31 '22 12:10

Chris Li