Skip to content

Instantly share code, notes, and snippets.

@webcyou
Created September 17, 2014 16:59
Show Gist options
  • Save webcyou/5f6e6b15e851028b4db2 to your computer and use it in GitHub Desktop.
Save webcyou/5f6e6b15e851028b4db2 to your computer and use it in GitHub Desktop.
get userAgent & version
var ua = navigator.userAgent,
uaFunc = function(s,h) { return (h.indexOf(s) > -1)};
var userInfo = {};
if(uaFunc("iPhone", ua)) {
userInfo.device = "iPhone";
} else if(uaFunc("iPad", ua)) {
userInfo.device = "iPad";
} else if(uaFunc("Android", ua)) {
userInfo.device = "Android";
} else {
userInfo.device = "PC";
}
if(userInfo.device == "iPhone") {
userInfo.ver = uaGetVer("iPhone OS", "like");
} else if(userInfo.device == "iPad") {
userInfo.ver = uaGetVer("CPU OS", "like");
} else if(userInfo.device == "Android") {
userInfo.ver = uaGetVer(userInfo.device, ";");
} else {
userInfo.ver = null;
}
function uaGetVer(myKey,contextEnd) {
var myStart = ua.indexOf(myKey) + myKey.length,
myEnd = ua.indexOf(contextEnd, myStart);
return "Ver." + ua.substring (myStart,myEnd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment