JS determines that the mobile side accesses the device and loads the corresponding CSS style

  • 2020-03-30 03:21:44
  • OfStack

JS judge different web access environments, mainly for mobile devices, to provide the corresponding solution (judge the device code directly copy qq.com)
 
//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)){ 

//If the access environment is Android|, webOS, |, iPhone, |, iPod, |, BlackBerry, then load the following styles

setActiveStyleSheet("style_mobile_a.css"); 

} 

else if(/iPad/i.test(navigator.userAgent)){ 

//If the access environment is iPad, load the following styles

setActiveStyleSheet("style_mobile_iPad.css"); 

} 

else{ 

//Determining that the access environment is another mobile device loads the following styles

setActiveStyleSheet("style_mobile_other.css"); 

} 

} 

catch(e){} 

} 

} 

else{ 

//If none of the above, load the following styles

setActiveStyleSheet("style_mobile_no.css"); 

} 

//After determining the loading style

function setActiveStyleSheet(filename){document.write(" The < link href="+filename+" rel=stylesheet > ");} 

Loading the page
 
<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> 

Related articles: