Front-end development often encountered the need to determine the user's browsing device, is the PC or mobile, mobile use what mobile phone system? Android, iOS, ipad, Windows Phone, and so on, sometimes you need to know whether the user browsing page is open in the mobile browser, and so on a series of judgments to do some corresponding processing.
1, first of all to determine the PC end or mobile side.
1 functionIsPC () {2 varUseragentinfo =navigator.useragent;3 varAgents = ["Android", "IPhone",4"SymbianOS", "Windows Phone",5"IPad", "IPod"];6 varFlag =true;7 for(varv = 0; v < agents.length; v++) {8 if(Useragentinfo.indexof (agents[v]) > 0) {9Flag =false;Ten Break; One } A } - returnFlag; -}
2. Determine the system platform used by the user on the mobile side
1 varU =navigator.useragent;2 if(U.indexof (' Android ') >-1 | | u.indexof (' Linux ') >-1) {3 //Android Phone4}Else if(U.indexof (' IPhone ') >-1) {5 //Apple Phone6}Else if(U.indexof (' Windows Phone ') >-1) {7 //Winphone Mobile8}
3. Determine if the user is open in
1 functionisweixin () {2 varUA =navigator.userAgent.toLowerCase ();3 if(Ua.indexof (' Micromessenger ')! =-1) { 4 return true; 5}Else { 6 return false; 7 } 8}
JS How to determine the user's device type and platform