...

View Full Version : Mouse events - moving from IE to Mozilla



Evios
05-08-2006, 07:12 PM
function hideMenu() {
if(activeHeader != null && activeMenu != null) {
if(!activeMenu.contains(event.toElement)) {
do stuff...
}
}
}


I'm unsure exactly how to rewrite this, to have Mozilla based browsers read this line.

I've tried with relatedTarget and currentTarget but to no avail. Any help would be appriciated!

Thanks

Beagle
05-08-2006, 07:35 PM
IE puts the event in window.event
Mozilla passes it as the first argument:

function handler(e)
{
var targ;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode;
}

For more info:

http://www.quirksmode.org/js/events_properties.html

jkd
05-08-2006, 07:59 PM
Firstly, you need to add in support for the contains() method:


HTMLElement.prototype.contains = function(element) {
if (element == document) return false;
if (element == this) return true;
else return this.parentNode.contains(element);
}


Then, event.toElement is roughly equivalent to the standard event.relatedTarget property.

Lastly, you need to get at the event object itself. If you bind the event listener via attachEvent and addEventListener, then IE's window.event object is passed in as an argument to the listener (emulating the standard model). If you attach it via document.onmouseout, then it isn't.

Example:



if (element.addEventListener) element.addEventListener("mouseout", handler, false);
else if (element.attachEvent) element.attachEvent("onmouseout", handler);

function handler(event) {
if(activeHeader != null && activeMenu != null) {
if(!activeMenu.contains(event.relatedTarget || event.toElement)) {
do stuff...
}
}
}


(as long as the HTMLElement prototyping code exists elsewhere on the page)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum