SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to create a generic addEventListener

    Hello,

    I just received the Javascript Anthology book... I saw the method to "addEventListener" and wonder if it could work in a generic way for any event.

    I tried to update it this way:

    function addEventListener( fn, evt )
    {
    if (typeof window.addEventListener != 'undefined')
    {
    window.addEventListener( evt, fn, false);
    }
    else if (typeof document.addEventListener != 'undefined')
    {
    document.addEventListener( evt, fn, false);
    }
    else if (typeof window.attachEvent != 'undefined')
    {
    window.attachEvent( evt, fn);
    }
    else
    {
    var oldfn = eval( 'window.on' + evt );
    if (typeof eval( 'window.on' + evt ) != 'function')
    {
    eval( 'window.on' + evt ) = fn;
    }
    else
    {
    eval( 'window.on' + evt ) = function() { oldfn(); fn(); };
    }
    }
    }

    I haven't tried it yet, I'm wondering if it could work on all kind of browsers, what's your advice ??

    Thanks !!
    Claude

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about trying it out? Open browser, open file and see if it works


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
  •