您的位置:首页 > Web前端 > JavaScript

js内核判断

2016-09-18 09:44 330 查看
以下常作为js判断浏览器内核的方式。
var browser = {
kernel: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.*/), //是否为移动终端
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 , //是否为iPhone或者QQHD浏览器
iPad: u.indexOf('iPad') > -1, //是否iPad
webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
sdk: u.indexOf('sdkVersion')>-1 //Android及IOS的Webview自定义UserAgent
};
}(),
language:(navigator.browserLanguage || navigator.language).toLowerCase()
};


若需要关闭浏览器窗口或在移动端打开浏览器时向移动端传递回调传递参数,可通过以下方式。

//关闭窗口
function closeWindow(code, msg){
setTimeout(function(){
if(browser.kernel.sdk){
document.getElementsByTagName("body")[0].style.display = "none";
notifySDK(code, msg);
}else if(browser.kernel.ios  || browser.kernel.iPhone || browser.kernel.iPad){
document.getElementsByTagName("body")[0].style.display = "none";
notifySDK(code, msg);
//window.close();
}else if(typeof WeixinJSBridge =='object'){//如果存在微信的js对象,则调用微信js关闭
WeixinJSBridge.invoke('closeWindow');
}else if(navigator.userAgent.indexOf("MSIE") > 0){//IE关闭页面处理
if(navigator.userAgent.indexOf("MSIE 6.0") > 0){
window.opener = null;
window.close();
}else{
window.open('', '_top');
window.top.close();
}
}else if(navigator.userAgent.indexOf("Firefox") > 0){
window.location.href = 'about:blank ';
}else{
document.getElementsByTagName("body")[0].style.display = "none";
window.opener = null;
window.open('', '_self', '');
window.close();
}
},1);
}
通知移动端调用浏览器内核app。
//通知SDK
function notifySDK(code,msg){
var urlStr = 'zzfPaySDK://business_close/{"code":"' + code + '","msg":';
if(typeof(msg) == "object"
&& (Object.prototype.toString.call(msg).toLowerCase() == "[object object]"
|| Object.prototype.toString.call(msg).toLowerCase() == "[object array]"))
urlStr += JSON.stringify(msg);
else{
try{
JSON.parse(msg);
urlStr += msg;
}catch(e){
urlStr += '"' + msg + '"';
}
}
urlStr += '}';
top.location.href = urlStr;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息