function runOnLoad() { BrowserDetect.detectBrowser(); } var BrowserDetect = { init: function () { // Detect browser... this.browser = this.searchString(this.dataBrowser) || "ein unbekannter Browser"; // Detect version... this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "eine unbekannte Version"; }, showWarning: function () { //special warning for IE8 document.getElementById('browser_warning').style.display = "block"; }, displayInfo: function () { document.getElementById('ua').innerHTML = navigator.userAgent; //display UserAgent document.getElementById('appv').innerHTML = navigator.appVersion; //display AppVersion document.getElementById('browser').innerHTML = this.browser; //display detected browser document.getElementById('version').innerHTML = this.version; //display detected version }, detectBrowser: function () { switch (this.browser) { case "ein unbekannter Browser": this.displayInfo(); break; case "Internet Explorer": if (this.version == "eine unbekannte Version") { } else if (this.version < 8) { this.displayInfo(); } else if (this.version == 8) { //I'm just picking on IE8 for sport this.displayInfo(); this.showWarning(); } else if (this.version > 8) { this.displayInfo(); } break; case "Chrome": if (this.version == "eine unbekannte Version") { } else if (this.version < 40) { this.displayInfo(); } break; case "Firefox": if (this.version == "eine unbekannte Version") { this.displayInfo(); } else if (this.version < 36) { this.displayInfo(); } break; case "Safari": if (this.version == "eine unbekannte Version") { } else if (this.version < 8) { this.displayInfo(); } break; case "Opera": if (this.version == "eine unbekannte Version") { } else if (this.version < 28) { this.displayInfo(); } break; default: this.displayInfo(); break; } }, searchString: function (data) { if (window.chrome) { //returns true for Chrome, Opera (15+) and Iron - but not Safari. document.getElementById("is_chrome").innerHTML = "true"; var lastItem = document.getElementById("browser_warning"); var chromeWarning = document.createElement("p"); chromeWarning.id = "chrome_special_warning"; chromeWarning.innerHTML = "Shortcut to Chrome detection?"; lastItem.parentNode.insertBefore(chromeWarning, lastItem); } if (window.opera) { //only Opera <=12 has window.opera //but will they reintroduce it in a future version? document.getElementById("is_opera").innerHTML = "true"; var lastItem = document.getElementById("browser_warning"); var operaWarning = document.createElement("p"); operaWarning.id = "opera_warning"; operaWarning.innerHTML = "Warning, you're using an older version of Opera."; lastItem.parentNode.insertBefore(operaWarning, lastItem); } else if (navigator.vendor === "") { //only Firefox supports navigator.vendor but leaves it empty. //It appears not to be possible to override this. //TODO: check if this is always true so we can depend on it. //If an older version of Firefox has a user-string "Firefox 100", //this will return Firefox version 100 document.getElementById('vendor').innerHTML = "<i>leerstring</i>" if (navigator.userAgent.indexOf("SeaMonkey") != -1) { //if SeaMonkey this.versionSearchString = "SeaMonkey"; return "SeaMonkey"; } else if (navigator.userAgent.indexOf("Firefox") != -1) { //if Firefox this.versionSearchString = "Firefox"; return "Firefox"; } else { // Mozilla-based browser that doesn't have Firefox in the useragent string return "Unkown Mozilla-based browser"; } } //navigator.vendor is supported by Webkit/Blink browsers: Safari, Chrome and Opera 15+ //but there is a motion to remove it from Blink. else if (navigator.vendor) { //if navigator.vendor exists, display it. document.getElementById('vendor').innerHTML = navigator.vendor.toString(); if (navigator.vendor.indexOf("Google") != -1) { //if Chrome this.versionSearchString = "Chrome"; return "Chrome"; } else if (navigator.vendor.indexOf("Apple") != -1) { //if Safari this.versionSearchString = "Version"; return "Safari"; } else if (navigator.vendor.indexOf("Opera") != -1) { //if Opera (15+) this.versionSearchString = "OPR"; return "Opera"; } else { //not Chrome or Safari but does have navigator.vendor! return "ein unbekannter Browser"; } } else { for (var i=0;i<data.length;i++) { var dataString = data[i].string; var dataProp = data[i].prop; this.versionSearchString = data[i].versionSearch || data[i].identity; if (dataString) { if (dataString.indexOf(data[i].subString) != -1) { return data[i].identity; } } else if (dataProp) { return data[i].identity; } } } }, 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: "SeaMonkey", identity: "SeaMonkey" },{ prop: window.opera, identity: "Opera", versionSearch: "Version" },{ string: navigator.userAgent, subString: "MSIE", identity: "Internet Explorer", versionSearch: "MSIE" }] }; BrowserDetect.init(); runOnLoad();
<p>This is a javascript browser-check that I'm updating because of IE11's user-agent string.</p> <p>Browser: <span id="browser">Initial</span></p> <p>Version: <span id="version">Initial</span></p> <hr/> <p>User-Agent String: <span id="ua">Initial</span></p> <p>navigator.appVersion: <span id="appv">Initial</span></p> <p>navigator.vendor: <span id="vendor">Initial</span></p> <p>if window.chrome: <span id="is_chrome">false</span></p> <p>if window.opera: <span id="is_opera">false</span></p> <p id="browser_warning">Warning: <span>You are using Internet Explorer 8</span></p>
#browser_warning { display:none; } [id*=warning] { color: #f00; } span { font-weight: 700; }