www.webdeveloper.com
Results 1 to 6 of 6

Thread: Cross Browser Event Listener

  1. #1
    Join Date
    Jan 2009
    Posts
    18

    Cross Browser Event Listener

    Here is the cross browser code in adding multiple events in an element inside a document.


    Code:
    var addEvent = function ( el , ev , func ) {
    	var evnt = el['on'+ev];
    	el['on'+ev]= function () {
    		if ( typeof evnt == "function" ) { evnt ();}
    		func ();
    	}
    };
    
    
    //example:
    function ex () {
     alert(2);
    }
    addEvent (window , "load" , (function(){alert(1);}));
    addEvent ( window, "load" , ex );

  2. #2
    Join Date
    Feb 2008
    Posts
    1,666
    First problem I notice is that you lose the functionality of the passed event control block. Another problem is that you provide no means of communicating with the actual event via a returned true or false value.

  3. #3
    Join Date
    Jan 2009
    Posts
    5
    I'm curious as to purpose of this code. Couldn't a conditional with addEventListener and attachEvent be used instead of a function for attaching events?

    if(window.addEventListener){
    window.addEventListener("load",function,false/true);
    }else if(window.attachEvent){
    window.attachEvent("onload",function);
    }

    Forgive me if I missed the question.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,666
    Quote Originally Posted by chickeneater View Post
    Forgive me if I missed the question.
    Some people are forever trying to re-invent the wheel.

  5. #5
    Join Date
    Oct 2008
    Posts
    349
    Quote Originally Posted by MrNobody View Post
    Some people are forever trying to re-invent the wheel.
    I wouldn't portray re-inventing the wheel as a bad thing. Just like every programmer has to write their own "hello world" program, every JavaScript developer should get acquainted with DOM before moving onto more abstract libraries.

  6. #6
    Join Date
    Feb 2008
    Posts
    1,666
    Quote Originally Posted by voidvector View Post
    I wouldn't portray re-inventing the wheel as a bad thing.
    I wouldn't -- as long as the re-invention is at least as good as the original. It must have some redeeming quality which makes it valuable over the original.

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles