Alright, so I needed a easy way to execute custom JavaScript for clients using versions of IE lower than 7. All the scripts Google showed me were really huge and quite outdated. So I took what I found, and turned it into something really lightweight. Note that this is just a quick hack, but so far it seems to be enough for my needs.
Code:
var Browser = {
Version: function() {
var version = 999; // we assume a sane browser
if (navigator.appVersion.indexOf("MSIE") != -1)
// bah, IE again, lets downgrade version number
version = parseFloat(navigator.appVersion.split("MSIE")[1]);
return version;
}
}
Which enables me to do stuff like this:
if (Browser.Version() < 7) {
... // if client is using IE6 or lower, run this code
}
..or this:
if (Browser.Version() >= 7) {
... // if client is using IE7 or a sane browser, run this code
}
It will simply return 999 if the browser is non-IE. Is it safe to assume that
navigator.appVersion
always will contain "MSIE" in uppercase?
25 comments:
thanks...
Thanks alot man! Worked good!
doesn't work for IE 8, I get version 7 as result
Thanks!
It worked for me in IE8. It will say 7 if you have IE8 in IE7 compatibility mode.
Slick. Just what I needed for a rush fix.
You are genius
thanks so much!!! finally a solution that really work!!
Thanks, worked fine.
Thanks! Works like a charm.
Bingo!
You Rock my Dear :-)
IE7 is still insane... only IE9 is sane in my opinion.
Just what I was looking for. Thanks!
var isIE=0/*@cc_on+ScriptEngineMajorVersion()*/;
works great... thanks a lot mate..
This is a nice minification:
function ieversion(a){return a=navigator.appVersion,a.indexOf('MSIE')+1?parseFloat(a.split('MSIE')[1]):999}
thanks a lot!
Very helpful, Thanks a lot :)
Nice work! Thanks a lot!!
works fine ;)
Thanks a lot
Nice and neat, shortest solution so far that I've found. Thanks!
stuff example
Nice one ! Thanks
In principle good script because it also takes the compatibility mode into account. But I found that, when building an alert into it, that that is fired in all browsers. I adapted it a bit and this way it works fine:
var ieBrowser = {
Version: function() {
var versionNr = 666; // :-)
if (navigator.userAgent.indexOf("MSIE") != -1) {
versionNr = parseFloat(navigator.appVersion.split("MSIE")[1]);
return versionNr;
}
else if (navigator.userAgent.indexOf("msie") != -1) {
versionNr = parseFloat(navigator.appVersion.split("msie")[1]);
return versionNr;
}
else return false;
}
}
if (ieBrowser.Version() >= 9) {
alert('IE 9 or higher');
}
I also made the script a bit more intuitive.
again dfoesnt work in IE 11
so many shitty code bits everywhere
Post a Comment