美文网首页
H5判断是浏览器or原生App Webview

H5判断是浏览器or原生App Webview

作者: 辣瓜瓜 | 来源:发表于2018-01-24 00:05 被阅读152次

在做活动页跳转的时候往往要求微商城需要跳转Web页面,安卓和苹果Webview内嵌的H5要跳转原生的App页面,这个时候需要判断当前所处的环境,这里需要设置安卓和苹果Webview的userAgent。
例:事先和安卓约定userAgent为android,和ios约定userAgent为ios,则h5端需要的代码:

var ua = navigator.userAgent.toLowerCase();
$(".godetail").on("click",function(){
     var pru = {goDetail: $(this).attr("data-id")};
     if(ua == "ios"){
        window.webkit.messageHandlers.goDetail.postMessage(pru);
     }else if(ua == "android"){
        window.android.jumpActivity(JSON.stringify(pru));
     }else{
        window.location.href = "${basePath}/item/"+$(this).attr("data-id")+".html";
     }
});

相关文章

网友评论

      本文标题:H5判断是浏览器or原生App Webview

      本文链接:https://www.haomeiwen.com/subject/kjakaxtx.html