JS judges different web access environments, mainly for mobile devices, and provides corresponding analysis solutions (judging device codes directly copy Tencent.com)
The code copy is as follows:
// Determine whether it is a mobile operating environment
if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
if(window.location.href.indexOf("?mobile")<0){
try{
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
// Determine that the access environment is Android|webOS|iPhone|iPod|BlackBerry, then load the following style
setActiveStyleSheet("style_mobile_a.css");
}
else if(/iPad/i.test(navigator.userAgent)){
// If you determine that the access environment is iPad, load the following style
setActiveStyleSheet("style_mobile_iPad.css");
}
else{
// If you determine that the access environment is another mobile device, the following style will be loaded
setActiveStyleSheet("style_mobile_other.css");
}
}
catch(e){}
}
}
else{
// If none of the above is loaded, the following style
setActiveStyleSheet("style_mobile_no.css");
}
// Loading style after judgment
function setActiveStyleSheet(filename){document.write("<link href="+filename+" rel=stylesheet>");}
Loading the page
The code copy is as follows:
<script type="text/javascript">
if(/AppleWebKit.*mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
if(window.location.href.indexOf("?mobile")<0){
try{
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
window.location.href="Mobile Page";
}else if(/iPad/i.test(navigator.userAgent)){
window.location.href="Tablet Page";
}else{
window.location.href="Other mobile pages"
}
}catch(e){}
}
}
</script>