SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    May 2013
    Posts
    202
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    using addEventListener in js when not supported how be progressive in my js code and

    using addEventListener in js when not supported how be progressive in my js code and provide equivalent? I need to use moderniz lib?

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,861
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Only Internet Explorer 8 (and the earlier dead versions) don't understand event listeners. Those browsers have an attachEvent method that works in a similar way and so all you need is to do to be able to use either of them to attach all your event listeners is to simply create a function that will call one or the other depending on which the browser supports and call that function instead.

    Here's an example of a function that will use whichever of the two that the browser supports (testing which of the two is supported once only the first tile you use it):

    Code:
    addEvent = function(ob, type, fn ) {
    if (window.addEventListener)
    addEvent = function(ob, type, fn ) {
      ob.addEventListener(type, fn, false );
    };
    else if (document.attachEvent)
    addEvent = function(ob, type, fn ) {
      var eProp = type + fn;
      ob['e'+eProp] = fn;
      ob[eProp] = function(){ob['e'+eProp]( window.event );};
      ob.attachEvent( 'on'+type, o[eProp]);
    };
    addEvent(ob, type, fn )
    };
    You'd then add all your event listeners using code such as:

    Code:
    addEvent(document.getElementById('myid'),
      'click',
      myfunction);
    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
    SitePoint Addict
    Join Date
    May 2013
    Posts
    202
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ajax.onload=processResponse()

    ajax.addEventListener("readystatechange", processResponse, true);

    // what about the same for^? exist any other statement for this(ajax callback) expect these two?


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
  •