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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Mouse events - moving from IE to Mozilla

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

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Firstly, you need to add in support for the contains() method:
    Code:
    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:

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


  •  

    Posting Permissions

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