Created
September 17, 2014 16:59
-
-
Save webcyou/5f6e6b15e851028b4db2 to your computer and use it in GitHub Desktop.
get userAgent & version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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