...

View Full Version : Code to check browser



Chris Hick
11-10-2010, 01:18 AM
I'll be the first to admit, I am not skilled with javascript, but I can do a little work here and there. I remember learning there is a way to detect a user's/viewer's browser. Is there a possible javascript that allows me to detect a viewer's browser, the based on the browser, selects css/html to use so that the website is viewed correctly?

Old Pedant
11-10-2010, 01:29 AM
http://www.w3schools.com/js/js_browser.asp

Chris Hick
11-10-2010, 03:19 AM
Old Pedant, I remember seeing that, but would you happen to know a code that doesn't write it but instead transfers it to another javascript that will display the correct layout.

Old Pedant
11-10-2010, 03:49 AM
???



var agent = navigator.userAgent.toLowerCase(); // lower case optional, but easier
if ( agent.indexOf("firefox") >= 0 )
{
... do something for firefox ...
} else if ( agent.indexOf("msie 8") >= 0 ) {
... for msie 8 ...
} else if ( agent.indexOf("msie 9") >= 0 ) {
... for msie 9 ...
} else if ( agent.indexOf("msie") >= 0 ) {
... for other msie ...
} ... etc. ...

what code you use for each browser has to be up to you.

Now, personally, I much prefer trying to write HTML that works the same in all browsers.

Unless you are doing something reasonably esoteric, it isn't all *THAT* hard.

Chris Hick
11-10-2010, 04:35 AM
I tried to do what I am doing for IE but if I mess with the code it messes up the other browsers.. IE is the only thing irritating me.. as always..

Philip M
11-10-2010, 08:41 AM
Browser detection using navigator.userAgent is unreliable.

To detect IE:-


<script type = "text/javascript">

var IE = ("v"=="\v");
alert (IE); // true or false

</script>


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

Old Pedant
11-10-2010, 10:04 PM
Browser detection using navigator.userAgent is unreliable.
Well, not to mention slower and clumsier.

Yes, if ALL you are concerned with is detecting IE, clearly Philip's answer is far superior. And sneakier.

mrhoo
11-10-2010, 10:59 PM
An added benefit is that IE9 does define the vertical tab escape, unlike the previous versions.
Philip has a simple way to separate any code that needs older IE's event or css treatments.

Philip M
11-11-2010, 08:34 AM
An added benefit is that IE9 does define the vertical tab escape, unlike the previous versions.
Philip has a simple way to separate any code that needs older IE's event or css treatments.

I have not got to IE9 yet.

Try this (I think it is Kor's)


<script type = "text/javascript">
var IE = /*@cc_on!@*/false;
alert (IE);
</script>

mrhoo
11-11-2010, 03:26 PM
Thanks, Philip, but I meant that IE 9 does not need to be handled with the older IE's- the same code that works in all the other browsers works fine in IE9,
including the newer Array methods and event and css styleSheet handling.

So your method separates the IE versions that need special code from the browsers that can run a 'standard' code..



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum