View Full Version : can't get javascript to detect IE6
petela
01-25-2003, 12:18 AM
Hi,
I have IE6 loaded on my machine, and I wrote a function which I want to work only in that browser version. When I run the code below, the function goes directly to the alert message - it doesn't detect IE6 on my computer. If I change the end of the variable to >=3 or >=4, the functions works just fine; in other words, it will detect that I have a version >= 3 or 4. If I use 5, I get the same result as I do with 6.
Any ideas?
Thanks,
Pete Layshock
var isIE6 = ((navigator.appName == "Internet Explorer") || navigator.appVersion.indexOf("MSIE") !=-1))
&& (parseInt(navigator.appVersion) >= 6) // test for IE6
function playMusic()
{
if (isIE6) // test for IE 6
{
document.worship.play()
}
else
{
var message = "Sorry, music available only in IE version 6."
alert(message)
}
}
brothercake
01-25-2003, 12:23 AM
Use an object test - "document.compatMode" will return undefined for anything except IE6
Skyzyx
01-25-2003, 06:18 PM
#1, you need to remember that navigator.appVersion begins with "4.0", which is why it'll work for 3 and 4, but not 5 or 6.
4.0 (compatible; MSIE 6.0; Windows NT 5.1)
#2, navigator.appName returns "Microsoft Internet Explorer", not just "Internet Explorer".
For #1, split the navigator.appVersion string at "MSIE". For example...
// Split the string into part [0] and part [1]
temp=navigator.appVersion.split('MSIE');
// Parse the string for the "6" in 6.0
ieVer=parseInt(temp[1]);
// Is it greater than 6?
var isIE6up=(ieVer >= 6)?1:0;
Something like that should work without getting all proprietary and junk...
petela
01-27-2003, 03:38 PM
Thank you very much for your feedback. I'll give that a try.
Sincerely,
Pete Layshock
whammy
01-28-2003, 01:26 AM
I'll agree with what Skyzyx said - this kind of stuff (navigator.appName and navigator.appVersion are perfect examples!) really needs to be standardized (and required to conform - ECMA perhaps?), regarding format, content, etc....
Browsers such as Opera should not be able to operate under the guise of a Microsoft browser, etc. - it defeats the whole purpose of the existence of those variables - and the fact that it's "OK" is just scary, I can see that with a virus or trojan horse, but not a browser. 'nuff said.
brothercake
01-28-2003, 10:57 AM
Slow down there - Opera's need for user-agent spoofing is a direct result of long-term over-reliance on navigator information, instead of the far more reliable object and feature detection, blunt instruments though they are.
Opera is no problem - because it always identifies itself in its UA string whatever else it may say; that's the perfect compromise - it can spoof its way past outdated detection scripts, but you can still identify it if you know its there.
Anger about UA sppofing would be better directed at the browsers which can do "true" spoofing - like OmniWeb, Espial Escape, ICEBrowser and Safari (but I have solutions for them, for those who are interested ...)
petela
01-28-2003, 03:40 PM
Thank you all for your replies.
petela
beetle
01-28-2003, 03:53 PM
Originally posted by brothercake
for those who are interested...Preach on brother(cake)
:D
brothercake
01-28-2003, 04:19 PM
:) Well..
ICEBrowser has a unique property - "navigator.__ice_version"
OmniWeb *always* returns true for "document.layers", but unlike netscape 4 it returns undefined for "navigator.mimeTypes['*']"
I shall have to check Escape and Safari info (it's at home - i'm at work) so I'll post that later on
MrDoubtFire
01-28-2003, 04:46 PM
Have you tried, 'document.getElementById' ?
elson
01-28-2003, 06:19 PM
Petela,
Try this sniffer, it works fine:
<head>
<script LANGUAGE="JavaScript">
<!-- hide JavaScript from non-JavaScript browsers
// This script is a simplified adaptation from the
// JavaScript Browser Sniffer
// Eric Krok, Andy King, Michel Plungjan
// see http://www.webreference.com/ for more information
var agt=navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();
var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);
var iePos = appVer.indexOf('msie');
if (iePos !=-1) {
is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)))
is_major = parseInt(is_minor);
}
var is_getElementById = (document.getElementById) ? "true" : "false";
var is_getElementsByTagName = (document.getElementsByTagName) ? "true" : "false";
var is_documentElement = (document.documentElement) ? "true" : "false";
var is_ie = ((iePos!=-1));
var is_ie3 = (is_ie && (is_major < 4));
var is_ie4 = (is_ie && is_major == 4);
var is_ie4up = (is_ie && is_minor >= 4);
var is_ie5 = (is_ie && is_major == 5);
var is_ie5up = (is_ie && is_minor >= 5);
var is_ie5_5 = (is_ie && (agt.indexOf("msie 5.5") !=-1));
var is_ie5_5up =(is_ie && is_minor >= 5.5);
var is_ie6 = (is_ie && is_major == 6);
var is_ie6up = (is_ie && is_minor >= 6);
// -->
</script>
</head>
brothercake
01-28-2003, 10:30 PM
So, ICEBrowser is sorted (it's actually not bad; possibly because it's based on a mozilla engine), and we can identify OmniWeb.
Espial Escape is the same - returns the same as OmniWeb for those two object tests, at all spoof settings.
Of course what that implies is that you can't reliably distinguish *between* OmniWeb and Escape ... but in practise you don't have to - they both suck, so personally I treat them both as though they have no DHTML support at all.
Safari is another matter ... it is a very good browser, but very hard to identify. As it goes, spoofing abilities aren't native to the browser; you need an add-on; but once you have it .. well .. safari can be almost anything .. even Windows IE6 !
I asked about this in the evolt mailing list, and PPK came up with "navigator.productSub", which at all settings returns 20021225
It works, but it's hardly ideal - because presumably it will change with each new public build - but it's the best I know of for now :cool:
whammy
01-28-2003, 11:31 PM
I wasn't targeting Opera per se - just the general idea that if these variables are there, why don't they use them (and correctly)? I mean, IE doesn't even show the proper version, etc.
brothercake
01-29-2003, 12:26 AM
Good point; maybe the real answer is not to rely on UA information at all; I mean object and feature detection are the best overal discriminators, but all browsers have bugs, and it's often only because of those bugs that you need to know more than feature support.
So given that UA information is erratic, I wonder if there's anything equally specific, but impossible to spoof?
... dunno .. but I just had a thought that amounts to a series of automated feature-detectors - something like, you test a load of values inside a supported-feature, and compare the return values with a look-up table of correct return values. Then you auto-compensate by redefining the behaviour of that feature!
I'm sure that's possible in theory ... but whether any browser would actually be able to do it in such a way that it would be useful ... but then, maybe that doesn't matter either - after all, degress of failure are feature detectors in their own right! if you can measure them :eek:
whammy
01-29-2003, 12:38 AM
Sounds like a good plan...
I'd just like to see even more standards adopted by browser makers in addition to XHTML, etc. that are truly useful.
Cross-browser compatibility, or an easier way to determine the lack thereof using ECMAScript (instead of having to test code in every browser) is a good start, even if they were to just make a single variable a standard that gave you an easy, true way to parse the browser/version...
It would make a developer's life a bit easier, especially regarding cross-browser stuff. I agree that the only reliable way to detect what a browser can do at this point is to basically test it and see what the results are; but it would be a lot simpler to just do a switch statement on a single variable or whatnot. :D
Of course, if they would all just conform to the standards that are OUT there, and we could get users to download the newest browsers, problem solved anyway. ;)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.