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");
}
base on this article, a cross-browser event handler can be :
You need to pass to
detachEvent
the function you added withattachEvent
. In your code you're passing a new one (they have the sametoString()
and would do the same thing but they're not the same).You should make
ff
global withand then call
to remove the listener in old IE.
Note : I'm really doubtful about the
document.body.removeAttribute("onclick");
: is there really a case in which this would be useful ?