Welcome to WebmasterWorld Guest from 54.226.147.190

Forum Moderators: open

Message Too Old, No Replies

Need a simple script to test for IE

     

londrum

5:30 pm on Oct 20, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



i know browser sniffing is old-hat these days, but i've come across a peculiar problem whereby firefox and IE will run a function at different times. the function works perfectly fine in all browsers, with no errors -- it just runs out of sequence. very strange.

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.

rocknbil

8:36 am on Oct 22, 2007 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Test for any MS-only object, the most valuable player being . . .

if (document.all) { alert('I\'m still IE at heart!'); }

I'm sure we'd all like to take a crack at the original problem though. :-)

londrum

7:13 pm on Oct 22, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



i've fixed the problem with that, so thanks for that tip.
but here's the original problem if you want to give it a try. be warned that it's given me a few headaches though!

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

rocknbil

7:40 pm on Oct 22, 2007 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Have not tried this - in a bit of a rush today, but it is possible your functions are trying to call elements that have not yet rendered?

Try moving these two to the bottom, and call window.onload:


window.onload = function () {
addLoadListener(first);
addLoadListener(eval("random" + Math.ceil(Math.random()*2)));
};

Fotiman

8:25 pm on Oct 22, 2007 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Why not do something like this, so you run them in order:

addLoadListener( function(){
first();
eval("random" + Math.ceil(Math.random()*2));
});

londrum

6:54 pm on Oct 24, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



had a go with both of those, but the eval() stops working if you wrap it in a function. there's no errors though - it just doesn't run.
it's got to be something to do with the eval()...

Fotiman

10:54 pm on Oct 27, 2007 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Try this:

addLoadListener( function(){
first();
eval("random" + Math.ceil(Math.random()*2) + "();");
});

londrum

12:49 pm on Oct 28, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



nope. tried every combination now but still comes up a blank.
is there a way of doing away with the eval completely, and doing the random digit a different way?

Fotiman

4:58 pm on Oct 29, 2007 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Maybe something like this:

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.

poppyrich

3:55 am on Oct 30, 2007 (gmt 0)

5+ Year Member



IE sniff extremely economical:

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.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month