PDA

View Full Version : Javascript Browser Sniffer



mswanndeford
Apr 4th, 2007, 08:49 PM
I would like to use a browser sniffer to detect IE7 so that I can use a different stylesheet to display my website specifically for IE7. If a user does not have IE7 then I want to display an alternate stylesheet.

Anyone have suggestions on how to do this? I have found lots of sniffer codes, but none that I can figure out how to modify. New here...

Thanks for any help!

ArcticFox
Apr 4th, 2007, 08:58 PM
You could try something like this:


<style>
// stuff for all browsers
body {background-color:#ff0000;}
</style>

<!--[if gte IE 7]>
<style>
// stuff for IE7+
body {background-color:#3366ff;}
</style>
<![endif]-->


As long as you put the conditional commented (http://www.javascriptkit.com/howto/cc.shtml) style after the other styles, it will override the previous in IE7+.

mswanndeford
Apr 4th, 2007, 09:22 PM
Yeah, I tried the IE comments. This issue with these is that when I tried IE7, it didn't work. I got it to work if I used IE6 instead, but then the rest of my browsers no longer rendered the design correctly.

ArcticFox
Apr 5th, 2007, 12:01 AM
How about something like this?:



<script language="JavaScript"><!--
browser_version= parseInt(navigator.appVersion);
browser_type = navigator.appName;

if (browser_type == "Microsoft Internet Explorer" && (browser_version >= 7)) {
document.write("<link rel='stylesheet' href='IE7.css' type='text/css'>");
}

// --></script>

mswanndeford
Apr 5th, 2007, 12:15 AM
Thanks so much, but it didn't work...IE7 doesn't recognize the alternate stylesheet.

:mad: Hate IE7...

ArcticFox
Apr 5th, 2007, 12:20 AM
Damn, I'm 0-2 today...



<script language="JavaScript"><!--
browser_version= parseInt(navigator.appVersion);
browser_type = navigator.appName;

if (browser_type == "Microsoft Internet Explorer" && (browser_version >= 7)) {
document.write("<link rel='stylesheet' href='IE7.css' type='text/css'>");
}

else {
document.write("<link rel='stylesheet' href='other.css' type='text/css'>");
}

// --></script>

rwedge
Apr 5th, 2007, 01:32 AM
In IE7 browser_version= parseInt(navigator.appVersion) returns 4.
You can use indexOf to check the appVersion for MSIE 7.0
if (navigator.appVersion.indexOf('MSIE 7.0') != -1) alert('IE7');

felgall
Apr 5th, 2007, 03:12 AM
Don't test any of the navigator fields. They are user configurable and will report most browsers as being Internet Explorer version 6, 7, 8, or 9.

Either use the HTML version of IE conditionals

<!--[if IE 7]>IE7 only<![endif]-->

or the JScript version

/*@cc_on
/*@if (@_jscript_version >= 5.7)
document.write("IE7");
/*@end
@*/

mswanndeford
Apr 5th, 2007, 06:42 PM
Thanks for the help...but I did try the IE comments and it doesn't work for IE7...any other ideas? :confused:

antimatter15
Apr 5th, 2007, 07:15 PM
Thanks for the help...but I did try the IE comments and it doesn't work for IE7...any other ideas? :confused:
what about:


if (navigator.userAgent.toLowerCase().indexOf('7.0') != -1){
//IE7 Code
}

felgall
Apr 5th, 2007, 10:33 PM
what about:


if (navigator.userAgent.toLowerCase().indexOf('7.0') != -1){
//IE7 Code
}



You can configure almost any web browser so that it will meet that condition and therefore run the IE7 only code.

All of the navigator fields are user enterable and can contain whatever the browser owner wants them to contain. You cannot rely on them for browser identification.

ArcticFox
Apr 6th, 2007, 12:30 AM
You can configure almost any web browser so that it will meet that condition and therefore run the IE7 only code.


If these people like to shoot themselves in the foot, that's their own problem when things don't show up right.



I think we may need to see all of your code, mswanndeford.

rwedge
Apr 6th, 2007, 02:10 AM
<!--[if IE 7]>IE7 only<![endif]-->

antimatter15
Apr 6th, 2007, 02:12 AM
You can configure almost any web browser so that it will meet that condition and therefore run the IE7 only code.

All of the navigator fields are user enterable and can contain whatever the browser owner wants them to contain. You cannot rely on them for browser identification. Yeah, but i'm sure 99.9% of normal internet users keep it as default.

MikeFoster
Apr 6th, 2007, 02:52 AM
IE conditional comments do work. If you are having problems with them post your code and we'll take a look.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test</title>

<style type='text/css'>
body {background-color:red;}
</style>

<!-- MS calls this "downlevel-hidden". Only IE7+ will get this: -->
<!--[if gte IE 7]>
<style type='text/css'>
body {background-color:green;}
</style>
<![endif]-->

<!-- MS calls this "downlevel-revealed". IE5, IE6 and all other browsers will get this: -->
<![if lt IE 7]>
<style type='text/css'>
body {background-color:blue;}
</style>
<![endif]>

</head>
<body>
<h1>Test</h1>
<p><a href='http://msdn.microsoft.com/workshop/author/dhtml/overview/ccomment_ovw.asp'>About Conditional Comments</a></p>
</body>
</html>

ArcticFox
Apr 6th, 2007, 04:01 AM
That's what I thought. I don't know why it's not working for mswanndeford... there has to be some other issue in the code.



Good to see that you actually read the thread before posting, rwedge. :rolleyes: That was the first reply.



<!--[if gte IE 7]>
<style>
// stuff for IE7+
body {background-color:#3366ff;}
</style>
<![endif]-->

felgall
Apr 6th, 2007, 11:55 AM
Yeah, but i'm sure 99.9% of normal internet users keep it as default.

100% of IE users will leave it at the default of IE. 99.9% of users of other browser will also have it set to IE because there are too many web pages that don't work when it is set to anything else.

rwedge
Apr 7th, 2007, 01:12 AM
Good to see that you actually read the thread before posting, rwedge. That was the first reply.

I was pointing out the error in Fegall's posted code like I did for your browser_version