|
Detecting IE using the navigator
object is challenging because of the “extra” information
stored by IE inside both navigator.appVersion and navigator.userAgent.
If we were to use parseFloat to
try and extract the browser version, we would get 4.0, instead of
5.5. This is due to the way parseFloat works - it returns the first
number it encounters
To retrieve the version number in IE,
utilize any combination of string methods that successfully extracts
this information. If you're detecting a single IE version (ie: IE
5.5 and NOT IE5.5+), the string method string.indexOf() will work.
//detect IE5.5
if (navigator.appVersion.indexOf("MSIE 5.5")!=-1)
alert("You are using IE5.5!")
However, it is sometimes necessary
to determine an entire range of browser versions (IE 5.5 and up,
for example). Our suggestion is to invoke the adage "whatever
works." Here is our method to detect a version of IE and up:
//Detect IE5.5+
version=0
if (navigator.appVersion.indexOf("MSIE")!=-1){ temp=navigator.appVersion.split("MSIE")
version=parseFloat(temp[1])
}
if (version>=5.5//NON IE browser
will return 0
alert("You are using IE5.5+")
In the above, string.split() is first
used to divide navigator.appVersion into two parts, using "MSIE" as
the separator. As a result, temp[1] contains the code after "MSIE",
with the browser version appearing first. Doing a parseFloat() on
temp[1] will retrieve the browser version of IE.
|