SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy addEvent handler with Safari 4

    Hi, I am having one problem writing my addEventHanlder that works with FF, IE and Safari.

    somehow, when I test my function with Safari

    alert(typeof(window.event)); // i can see that safari display: [object event]
    though, Safari does not support
    HTML Code:
    attachEvent
    do you have any idea to correct this below function?


    HTML Code:
    addEventHandler: function(oNode, sEvt, fnHandler, bCapture) {
            if (typeof (window.event) != "undefined")
                oNode.attachEvent("on" + sEvt, fnHandler);
            else
                oNode.addEventListener(sEvt, fnHandler, bCapture);
        }

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,785
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Switch the if and else around the other way so that only IE fails the if.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Switch the if and else around the other way so that only IE fails the if.
    His custom method would still be prone to IE not supporting 'this' when using attachEvent, and there's a better way of doing this, my personal minimal addEvent function:

    Code:
        var addEvent = (function() {
    	function addEventIE(el, ev, fn) {
    	    return el.attachEvent('on' + ev, function(e) {
    		return fn.call(el, e);
    	    });
    	}
    	function addEventW3C(el, ev, fn) {
    	    return el.addEventListener(ev, fn, false);
    	}
    	return window.addEventListener ? addEventW3C:addEventIE;
        })();
    The checks for whether the element supports addEventListeners or not is optimized by querying whether the window actually supports the method, unlike how many/all mainstream functions do it ( dustin diaz, dean edwards, john resigs versions could be further optimized and I believe one or two of them don't account for the broken this keyword in IE ).

    However this is something I worked on 2 or so years ago, I might add support for removing events and having a dictionary set of event handlers attached and maybe captures.
    Cross browser css bugs

    Dan Schulz you will be missed

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, I really appreciate your fast responses


Bookmarks

Posting Permissions

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