SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Mar 2004
    Location
    Belgrade, Serbia
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Prototype addEventListener for IE?

    I wonder is it possible to generally prototype event listeners functions for IE?

    Something like...

    Code:
    if ( typeof(HTMLElement) != "undefined" ) {
    	if ( !(HTMLElement.prototype.addEventListener) ) {
    		HTMLElement.prototype.addEventListener = function(sEv,sFn,bCap) {
    			if (this.attachEvent) {
    				this.attachEvent("on" + sEv, sFn);
    			}
    		};
    	}
    }
    Of course, IE doesn`t support HTMLElement. Can this be done any other way?

    I want to simply write oObj.addEventListener() and that it works in IE too. If that is not too much to ask...

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've experimented with similar things. But I use the DOM2 event model less and less. What you're wanting is as simple as this:

    element.onclick = myFunction;

    One of the many benefits to using the DOM0 event model is that, inside myFunction this points to element. That's not always the case when using addEventListener().

    I'm finally starting to realize that the only time anyone should use the DOM2 event model is when they want more than one listener on the same object. And even then, there are other ways - I've experiment with code for this too

    Your project sounds very interesting - I'm not trying to talk you out of it, its just that I've been thinking about this lately.



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
  •