html5移动端网站常用判断js代码

2018年04月27日 15:51:32益点益滴4211

1,判断识别是否为手机移动端运行环境

var mob = '';  
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{   
  mob = 'true';   
  }   
  catch(e){}   
  }   
}else{   
mob = 'false';  
};

2,获取当前页面网址链接参数

function getQueryString(name) {  
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");  
var r = window.location.search.substr(1).match(reg);  
if (r != null) return unescape(r[2]);  
return null;  
};

3,获取页面视窗宽高

function getViewRect() {  
var pageWidth = window.innerWidth  
,pageHeight = window.innerHeight;  
  
if ( typeof pageWidth != 'number' ) {  
if ( document.compatMode == 'CSS1Compat') {  
pageWidth = document.documentElement.clientWidth;  
pageHeight = document.documentElement.clientHeight;  
} else {  
pageWidth = document.body.clientWidth;  
pageHeight = document.body.clientHeight;  
}  
}  
return {  
width: pageWidth,  
height: pageHeight  
};   
};

4,智能识别判断手机横竖屏状态

function hengshuping(){   
  if(window.orientation==180||window.orientation==0){   
var h = getViewRect().height;  
$('html').css('minHeight',h + 'px');      
   }   
  if(window.orientation==90||window.orientation==-90){   
var h = getViewRect().height;  
$('html').css('minHeight',h + 'px');            
}   
};

5,手机浏览器信息识别和判断跳转

var browser = {  
versions: function() {  
var u = navigator.userAgent, app = navigator.appVersion;  
return {//移动终端浏览器版本信息   
trident: u.indexOf('Trident') > -1, //IE内核  
presto: u.indexOf('Presto') > -1, //opera内核  
webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核  
gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核  
mobile: !!u.match(/AppleWebKit.*Mobile.*/) || !!u.match(/AppleWebKit/), //是否为移动终端  
ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端  
android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器  
iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器  
iPad: u.indexOf('iPad') > -1, //是否iPad  
webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部  
};  
}(),  
language: (navigator.browserLanguage || navigator.language).toLowerCase()  
}  
var ua = window.navigator.userAgent.toLowerCase();  
if(ua.match(/MicroMessenger/i) == 'micromessenger'){  
$('.wholePage').show();  
}else if(browser.versions.ios || browser.versions.iPhone || browser.versions.iPad){  
//$('#downMsg').show();  
location.href = 'https://itunes.apple.com/cn/app/id1149168395';  
}else{  
//$('#downMsg').show();  
location.href = "http://www.yiwuku.com";  
}

6,判断手机是android还是ios操作系统

//判断是否为ios端访问function _IsIOS() {
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {        return true;
    } else {        return false;
    }
}//判断是否为android端访问function _IsAndroid() {
    if (/(Android|Adr)/i.test(navigator.userAgent)) {        return true;
    } else {        return false;
    }
}
//method为移动端原生方法的方法名,param为要传递的参数if (_IsIOS()) {
    window.webkit.messageHandlers.method.postMessage(param);
} else if (_IsAndroid()) {
    window.control.call('method', param);    //或者window.control.method(param);}

超赞,真给力!嗯,必须鼓励~

打赏1
账号:mxy310@163.com[复制]
账号:77940140[复制]