判断网页是通过PC端还是移动终端打开的

通过判断打开设备,跳转不同页面,可以根据 User-Agent 来区分
if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {window.location.hrefhttps://www.it610.com/article/= ""; //手机} else {window.location.hrefhttps://www.it610.com/article/= ""; //电脑}
也可以执行其他操作:
if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {alert('您正在通过手机访问'); } else {alert("您在PC端访问"); }
JS判断客户端是否是iOS或者Android手机移动端:
通过判断浏览器的 userAgent,用正则来判断手机是否是ios和Android客户端。代码如下:

下面一个比较全面的浏览器检查函数,提供更多的检查内容,你可以检查是否是移动端(Mobile)、ipad、iphone、微信、QQ等。
第一种:

检测浏览器语言
currentLang = navigator.language; //判断除IE外其他浏览器使用语言if(!currentLang){//判断IE浏览器使用语言currentLang = navigator.browserLanguage; }alert(currentLang);
第二种:
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {//alert(navigator.userAgent); window.location.href ="https://www.it610.com/article/iPhone.html"; } else if (/(Android)/i.test(navigator.userAgent)) {//alert(navigator.userAgent); window.location.href ="https://www.it610.com/article/Android.html"; } else {window.location.href ="https://www.it610.com/article/pc.html"; };
也可以通过这样来适配,然后直接转跳到移动端页面:
【判断网页是通过PC端还是移动终端打开的】function mobile_device_detect(url){var thisOS=navigator.platform; var os=new Array("iPhone","iPod","iPad","android","Nokia","SymbianOS","Symbian","Windows Phone","Phone","Linux armv71","MAUI","UNTRUSTED/1.0","Windows CE","BlackBerry","IEMobile"); for(var i=0; i

    推荐阅读