...

View Full Version : Event listener order in IE



Martins
01-06-2012, 08:30 AM
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.


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);

devnull69
01-06-2012, 08:36 AM
Yes


addEvent(window, 'load', function() {
function1();
function2();
function3();
function4();
function5();
});

Amphiluke
01-06-2012, 10:41 AM
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

DaveyErwin
01-06-2012, 10:49 AM
Looks like flat nonsenseÖ O__o

Not nonsense at all


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/

Amphiluke
01-06-2012, 10:53 AM
Well, now I see, thanks.

Martins
01-06-2012, 05:44 PM
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.

felgall
01-06-2012, 07:19 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum