Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts

    IE and Firefox event support with multiple variables

    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:
    Code:
    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.
    Last edited by BarrMan; 09-01-2008 at 10:56 AM.

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    It's the inner function that becomes the event handler and which receives the event object.

  • #3
    New Coder
    Join Date
    Jul 2008
    Location
    Peterborough - UK
    Posts
    63
    Thanks
    4
    Thanked 9 Times in 9 Posts
    I use the following little gem:
    Code:
    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:

    Code:
    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

  • #4
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by Arty Effem View Post
    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.

  • #5
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by BarrMan View Post
    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:
    Code:
    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
    Last edited by Arty Effem; 08-31-2008 at 08:56 PM.

  • Users who have thanked Arty Effem for this post:

    BarrMan (08-31-2008)

  • #6
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Thanks Arty. That worked!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •