homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Need a simple script to test for IE

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

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.



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

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. :-)


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

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


function random1(){
var boxtwo=document.createElement('div');
var place=document.getElementById('footer');
function random2(){
var boxthree=document.createElement('div');
var place=document.getElementById('footer');

addLoadListener(eval("random" + Math.ceil(Math.random()*2)));


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

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(eval("random" + Math.ceil(Math.random()*2)));


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

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

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


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

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()...


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

Try this:

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


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

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?


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

Maybe something like this:

addLoadListener( function(){
alert("start first()...");
alert("done first()");
var n = Math.ceil(Math.random()*2);
alert("switch " + n);
switch(n) {
case 1:
alert("start random1()...");
alert("done random1()");
case 2:
alert("start random2()...");
alert("done random2()");

Note, I added some alerts to help you debug.


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

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() {

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved