So, everyone knows IE’s existence is easily worse than anything Hitler ever did. I needed to be able to detect what versions of browsers were coming to my site and give them an playful suggestion for them upgrade their browser, or face potential disgusting css bugs. So… I wrote this script here, that I feel is a bit easier to understand than many other javascript people have found once upon a google.
Place this in a file, I called it browser.js, and include it in your <head> tag.
Then do something like
if (isClientBrowserTooOld()) alert("I'm sorry Dave, but your browser is older than you are. You'll need to download the latest version of your browser before I can let you continue, Dave");
or something to that effect.
And for zhe script:
function isClientBrowserTooOld(){
var ua = window.navigator.userAgent
var ie = msieVersion(ua);
var chrome = chromeVersion(ua);
var firefox = firefoxVersion(ua);
var safari = safariVersion(ua);
//alert(ie + ":" + chrome + ":" + firefox + ":" + safari); //debugging
if (ie < 7.0 && ie > 0) return true;
else if (chrome < 5.0 && chorme > 0) return true;
else if (firefox < 3.4 && firefox > 0) return true;
else if (safari < 4 && safari > 0) return true;
else return false;
}
// Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)
function msieVersion(ua){
var msie = ua.indexOf("MSIE ");
if ( msie > 0 ) // If Internet Explorer, return version number
return parseInt(ua.substring(msie+5, ua.indexOf(".", msie )));
else // If another browser, return 0
return 0;
}
// Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.475.0 Safari/534.3
function chromeVersion(ua){
var chrome = ua.indexOf("Chrome/");
if (chrome > 0) return parseInt(ua.substring(chrome+7, ua.indexOf(".", chrome) + 1)); // returns 6.0
else return 0;
}
// Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0b2) Gecko/20100720 Firefox/4.0b2
function firefoxVersion(ua){
var ff = ua.indexOf("Firefox");
if (ff > 0) return parseInt(ua.substring(ff+8, ua.indexOf(".", ff) + 1)); // returns 4.0
else return 0;
}
// Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-us) AppleWebKit/533.17.8 (KHTML, like Gecko) Version/5.0.1 Safari/533.17.8
function safariVersion(ua){
var safari = ua.indexOf("Version");
if (safari > 0 && ua.indexOf("Safari") > 0) return parseInt(ua.substring(safari+8, ua.indexOf(".", safari) + 1)); // returns 5.0
else return 0;
}
Comments
Leave a comment Trackback