Benutzer: Gast
Eintrag: JavaScript Browser-Detection

JavaScript Browser-Detection

von: marcel
Kreise: HTML / CSS / JS
Code: JS JScript JavaScript
/**
 * detect browser and version - then add css-classes to html-tag
 */
var ua = navigator.userAgent.toLowerCase(),
	browser = "",
	version = 0;
if (/chrome/.test(ua)) {
	browser = "chrome";
	version = parseInt(ua.substring(ua.indexOf(browser) + 7).split(".")[0]);
} else if (/safari/.test(ua)) {
	browser = "safari";
	version = parseInt(ua.substring(ua.indexOf("version") + 8).split(".")[0]);
} else if (/webkit/.test(ua)) {
	browser = "webkit";
} else if (/opera/.test(ua)) {
	browser = "opera";
	version = parseInt(ua.substring(ua.indexOf("version") + 8).split(".")[0]);
} else if (/msie/.test(ua)) {
	browser = "msie";
	version = parseInt(ua.split(browser)[1]);
} else if (/firefox/.test(ua)) {
	browser = "firefox";
	version = parseInt(ua.substring(ua.indexOf(browser) + browser.length + 1).split(".")[0]);
}
if (browser) {
	document.getElementsByTagName("html")[0].className += " browser_" + browser + " browser_" + browser + "_" + version.toString();
}
Es gibt noch keine Kommentare.