/ Published in: JavaScript
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* * Andrew Wayne 2010 * Browser detection with javascript * this has been passed through JSLint */ var Browser = { init: function () { // Get the browser info this.browser = this.searchString(this.dataBrowser) || "unknownBrowser"; this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "unknownVersion"; this.OS = this.searchString(this.dataOS) || "unknownOS"; }, // search through the dataBrowser and dataOS object to find a match // for the browser name and OS respectively searchString: function (data) { var dataString, dataProp, i=0, len = data.length; for (i; i<len; i+=1) { dataString = data[i].string; dataProp = data[i].prop; this.versionSearchString = data[i].versionSearch || data[i].agent; if (dataString) { if (dataString.indexOf(data[i].subString) !== -1) { return data[i].identity; } } else if (dataProp) { return data[i].identity; } } }, // search through the userAgent to find a match // for the browser version searchVersion: function (dataString) { var index = dataString.indexOf(this.versionSearchString); if (index === -1) { return; } return parseFloat(dataString.substring(index+this.versionSearchString.length+1)); }, dataBrowser: [ { string: navigator.userAgent, subString: "Chrome", agent: "Chrome", identity: "chrome" }, { string: navigator.userAgent, subString: "OmniWeb", versionSearch: "OmniWeb/", agent: "OmniWeb", identity: "omniweb" }, { string: navigator.vendor, subString: "Apple", agent: "Safari", identity: "safari", versionSearch: "Version" }, { prop: window.opera, agent: "Opera", identity: "opera" }, { string: navigator.vendor, subString: "iCab", agent: "iCab", identity: "icab" }, { string: navigator.vendor, subString: "KDE", agent: "Konqueror", identity: "konqueror" }, { string: navigator.userAgent, subString: "Firefox", agent: "Firefox", identity: "ff" }, { string: navigator.vendor, subString: "Camino", agent: "Camino", identity: "camino" }, { // for newer Netscapes (6+) string: navigator.userAgent, subString: "Netscape", agent: "Netscape", identity: "netscape" }, { string: navigator.userAgent, subString: "MSIE", agent: "Explorer", identity: "ie", versionSearch: "MSIE" }, { string: navigator.userAgent, subString: "Gecko", agent: "Mozilla", identity: "mozilla", versionSearch: "rv" }, { // for older Netscapes (4-) string: navigator.userAgent, subString: "Mozilla", agent: "Netscape", identity: "netscape", versionSearch: "Mozilla" } ], dataOS : [ { string: navigator.platform, subString: "Win", agent: "Windows", identity: "win" }, { string: navigator.platform, subString: "Mac", agent: "Mac", identity: "mac" }, { string: navigator.userAgent, subString: "iPhone", agent: "iPhone/iPod", identity: "iphone-ipod" }, { string: navigator.platform, subString: "Linux", agent: "Linux", identity: "linux" } ] }; // Example usage Browser.init(); document.write('<h1>You\'re using ' + Browser.browser + ' ' + Browser.version + ' on ' + Browser.OS + '!</h1>');