Skip to content

Instantly share code, notes, and snippets.

@RobertWang
Created May 9, 2014 02:53
Show Gist options
  • Save RobertWang/ca7a663e21356d7d9b2d to your computer and use it in GitHub Desktop.
Save RobertWang/ca7a663e21356d7d9b2d to your computer and use it in GitHub Desktop.
[js]判断浏览器
function sys_userAgent(){
var userAgent = navigator.userAgent,
rMsie = /(msie\s|trident.*rv:)([\w.]+)/,
rFirefox = /(firefox)\/([\w.]+)/,
rOpera = /(opera).+version\/([\w.]+)/,
rChrome = /(chrome)\/([\w.]+)/,
rSafari = /version\/([\w.]+).*(safari)/;
var browser,version,ua;
ua = userAgent.toLowerCase();
var match = rMsie.exec(ua);
if (match != null) {
return { browser : "ie", version : match[2] || "0" };
}
if (!!window.ActiveXObject || "ActiveXObject" in window){
return { browser : "ie", version : "0"};
}
var match = rFirefox.exec(ua);
if (match != null) {
return { browser : "firefox", version : match[2] || "0" };
}
var match = rOpera.exec(ua);
if (match != null) {
return { browser : "opera", version : match[2] || "0" };
}
var match = rChrome.exec(ua);
if (match != null) {
return { browser : "chrome", version : match[2] || "0" };
}
var match = rSafari.exec(ua);
if (match != null) {
return { browser : "safari", version : match[1] || "0" };
}
if (match != null) {
return { browser : "", version : "0" };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment