Forum Moderators: open
after trying everything to fix it, i have found that the only way to solve the problem is to sniff for anyone using IE, and then run the function in a different place in the script.
so i was wondering if anyone has got a simple script that sniffs for IE only - just IE. and it doesn't matter what version they're running either. any version of IE will do.
if anyone has got something like that then they will be my eternal hero.
this code is vastly simplified, but it includes the basic stuff.
basically, i have a function called 'first' which prints some code to the page. and i've stuck it in an addLoadListener, so it runs after the page has loaded.
i then have another couple of functions which write some more code to the page, but they are randomised, so only one of them is actually selected. i choose between them by using eval(). and i've stuck the eval() in the addLoadListener as well.
Firefox will run the functions in the order given in the script, but IE runs them in reverse order. so the code that has been written to the page appears in reverse order to.
both functions do work okay though - they run with no errors. i haven't got a clue why it would happen like that, but i'm guessing that the eval() function must be taking precedence over everything else.
function first(){
var boxone=document.createElement('div');
var place=document.getElementById('footer');
place.parentNode.insertBefore(boxone,place);
} addLoadListener(first); function random1(){
var boxtwo=document.createElement('div');
var place=document.getElementById('footer');
place.parentNode.insertBefore(boxtwo,place);
}
function random2(){
var boxthree=document.createElement('div');
var place=document.getElementById('footer');
place.parentNode.insertBefore(boxthree,place);
} addLoadListener(eval("random" + Math.ceil(Math.random()*2)));
Try moving these two to the bottom, and call window.onload:
window.onload = function () {
addLoadListener(first);
addLoadListener(eval("random" + Math.ceil(Math.random()*2)));
};
addLoadListener( function(){
alert("start first()...");
first();
alert("done first()");
var n = Math.ceil(Math.random()*2);
alert("switch " + n);
switch(n) {
case 1:
alert("start random1()...");
random1();
alert("done random1()");
break;
case 2:
alert("start random2()...");
random2();
alert("done random2()");
break;
}
});
Note, I added some alerts to help you debug.
var isMSIE = /*@cc_on!@*/false;
Something like this might help with the reordering problem:
(This code not original to me. Search more - it's a common problem problem.)
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload!= 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
addLoadEvent(function() {
/* more code to run on page load */
});
Also, I didn't look your code over carefully but be aware that eval works within the same scope chain as any other method you might use.
It might not be seeing the variable values.