...

View Full Version : Resolved IE and Firefox event support with multiple variables



BarrMan
08-31-2008, 12:32 PM
Hey,
I'm trying to make a DropDown menu but I'm having a bit of a problem on doing so.
I'm trying to create a function which contains the event variable for firefox (e) and also a second variable for the obj.

I'm not sure how to achieve both at the same time.
Here's what I've got so far:

obj.onmouseout = (function(opts,e)
{
var oThis = this;
e = typeof(e)=='undefined'?window.event:e;
return function()
{
if(document.all&&!opts.contains(e.toElement))
tmr = window.setTimeout(function(){closemenu(opts);},delay);
else if(!contains(opts, e.relatedTarget))
tmr = window.setTimeout(function(){closemenu(opts);},delay);
return false;
}
})(opts,e);

The code above does not work, it informs of an error of unknown variable e.

Thanks.

Arty Effem
08-31-2008, 03:58 PM
It's the inner function that becomes the event handler and which receives the event object.

Mikebert4
08-31-2008, 06:30 PM
I use the following little gem:


function my_func(e){
if (!e) {var e = window.event;}
if(document.attachEvent){
var elm = e.srcElement;
}else{
var elm = e.target;
}

}


the variable 'e' is the event, and the variable 'elm' is the element that fired the event - everything you'd need when playing around with attaching events. All you do is add your code to this function and then attach it to your desired object using:




var obj = <<your object here>>;
if(document.attachEvent){
obj.attachEvent('onload', function(e) {myfunc(e);});
}else if(document.addEventListener){
obj.addEventListener('load', function (e) {myfunc(e);}, false);
}else{
obj.onload = function(e) {my_func(e);};
}


obviously, 'onload and 'load' can be changed to 'onclick' and 'click' or 'onmouseover' and 'mouseover'... etc etc.

hope this helps

BarrMan
08-31-2008, 06:54 PM
It's the inner function that becomes the event handler and which receives the event object.

Hey, Thanks for the reply. But I don't quite get what you mean.

Arty Effem
08-31-2008, 08:48 PM
Hey, Thanks for the reply. But I don't quite get what you mean.I mean that the inner function should be coded to receive the event, not the outer function which is what you have.
Presumably you want something closer to this:

obj.onmouseout = (function(opts)
{
return function(evt)
{
var e = evt || window.event;
if(document.all&&!opts.contains(e.toElement))
tmr = window.setTimeout(function(){closemenu(opts);},delay);
else if(!contains(opts, e.relatedTarget))
tmr = window.setTimeout(function(){closemenu(opts);},delay);

return false;
}
})(opts); // or possibly this

BarrMan
08-31-2008, 09:10 PM
Thanks Arty. That worked! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum