js内核判断

以下常作为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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值