Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    73
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Event listener order in IE

    The following code runs the functions in the order 1,2,3,4,5 (testing with current versions of FF, Safari, Chrome) but in IE8 (I donít have IE9) they run as 3,2,4,5,1?

    Is there a solution to run them in the order presented with IE?

    Thanks.

    Code:
    	function addEvent( obj, type, fn ){ 
    	   if (obj.addEventListener){ 
    	      obj.addEventListener( type, fn, false );
    	   }
    	   else if (obj.attachEvent){ 
    	      obj["e"+type+fn] = fn; 
    	      obj[type+fn] = function(){ obj["e"+type+fn]( window.event ); } 
    	      obj.attachEvent( "on"+type, obj[type+fn] ); 
    	   } 
    	} 
    	
    	addEvent(window, 'load', function1);
    	addEvent(window, 'load', function2);
    	addEvent(window, 'load', function3);
    	addEvent(window, 'load', function4);
    	addEvent(window, 'load', function5);

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Yes
    Code:
    addEvent(window, 'load', function() {
       function1();
       function2();
       function3();
       function4();
       function5();
    });

  • Users who have thanked devnull69 for this post:

    Martins (01-06-2012)

  • #3
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    Quote Originally Posted by Martins
    Code:
    else if (obj.attachEvent){ 
        obj["e"+type+fn] = fn; 
        obj[type+fn] = function(){ obj["e"+type+fn]( window.event ); }
    Looks like flat nonsense… O__o
    I am still learning English

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Quote Originally Posted by Amphiluke View Post
    Looks like flat nonsenseÖ O__o
    Not nonsense at all

    Code:
    function addEvent( obj, type, fn ) {
      if ( obj.attachEvent ) {
        obj['e'+type+fn] = fn;
        obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
        obj.attachEvent( 'on'+type, obj[type+fn] );
      } else
        obj.addEventListener( type, fn, false );
    }
    function removeEvent( obj, type, fn ) {
      if ( obj.detachEvent ) {
        obj.detachEvent( 'on'+type, obj[type+fn] );
        obj[type+fn] = null;
      } else
        obj.removeEventListener( type, fn, false );
    }
    from http://ejohn.org/projects/flexible-javascript-events/

  • Users who have thanked DaveyErwin for this post:

    Amphiluke (01-06-2012)

  • #5
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    Well, now I see, thanks.
    I am still learning English

  • #6
    New Coder
    Join Date
    Nov 2005
    Posts
    73
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Devnull69, thanks very much for your help with this and such a swift response, it has been a great help.

    So far I have tested in IE6, IE7, IE8, FF8, Chrome16, Safari5.1 and Opera11. It works correctly in all of them.

    Cheers.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,472
    Thanks
    0
    Thanked 634 Times in 624 Posts
    There is no guaranteed order in which event listeners are supposed to run so if you want code to run in a particular order you need all that code within the one listener. You use multiple listeners where the code is unrelated and it doesn't matter which order they run in.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    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
    •