homepage Welcome to WebmasterWorld Guest from 54.196.62.23
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
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
londrum

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3482827 posted 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.

 

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3482827 posted 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. :-)

londrum

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3482827 posted 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');
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

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3482827 posted 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(first);
addLoadListener(eval("random" + Math.ceil(Math.random()*2)));
};

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3482827 posted 8:25 pm on Oct 22, 2007 (gmt 0)

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

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

londrum

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3482827 posted 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()...

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3482827 posted 10:54 pm on Oct 27, 2007 (gmt 0)

Try this:

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

londrum

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3482827 posted 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?

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3482827 posted 4:58 pm on Oct 29, 2007 (gmt 0)

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

5+ Year Member



 
Msg#: 3482827 posted 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() {
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.

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