热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

京东在html5页面中翻开当地app的解决方案

从html5翻开当地的app–假如当地没有app就跳转到下载页面,人人都邑以为这是一项很简单的操纵。网上的教程也许多,然则可行性都不高。由于手机体系和浏览器型号各不相同,所以兼容性

从html5翻开当地的app–假如当地没有app就跳转到下载页面,人人都邑以为这是一项很简单的操纵。网上的教程也许多,然则可行性都不高。由于手机体系和浏览器型号各不相同,所以兼容性会是让各个前端工程师头疼的题目。我们无妨看一下京东是怎样处理的。京东的原代码已殽杂过了,我只能一点点反殽杂并解释。

网上的文章一模一样 都是采纳window.location.href的体式格局翻开的,然则这类要领的兼容性异常的渣。在ios的safari浏览器中没法运用,会涌现还未翻开app就自动跳转到下载页面的状况,影响用户的运用。那末我们来看一下京东是怎样处理兼容性的题目。
下面附上我的代码翻译和解释。
京东翻开app的js代码链接


(function(){
// 推断浏览器
var Navigator = navigator.userAgent;
var ifChrome = Navigator.match(/Chrome/i) != null && Navigator.match(/Version\/\d+\.\d+(\.\d+)?\sChrome\//i) == null ? true : false;
var ifAndroid = (Navigator.match(/(Android);?[\s\/]+([\d.]+)?/)) ? true : false;
var ifiPad = (Navigator.match(/(iPad).*OS\s([\d_]+)/)) ? true : false;
var ifiPhOne= (!ifiPad && Navigator.match(/(iPhone\sOS)\s([\d_]+)/)) ? true : false;
var ifSafari = (ifiPhone || ifiPad) && Navigator.match(/Safari/);
var version = 0;
ifSafari && (version = Navigator.match(/Version\/([\d\.]+)/)); version = parseFloat(version[1], 10);
// 是不是从微信翻开
var ifWeixin = navigator.userAgent.indexOf("MicroMessenger") >= 0; // weixin
var j = false;
var iframe = "plugIn_downloadAppPlugIn_loadIframe";
var t = false;
var i = 0;
var B = {};
var b = {};
var selector = null;
var Hquery = {};
// 推断当前运用的js框架是zepto照样jquery
var Query = window.Zepto || window.jQuery ? true : false;
var g = [];
// 是不是存在html5的localStorage 存储
var v = window.localStorage ? true : false;
var o = "mdownloadAppPlugInskip";
var p = null;

function m() { // 打印时候 比方:2016-5-18
var M = new Date();
var N = M.getFullYear();
var O = M.getMonth() + 1;
var L = M.getDate();
strDate = N + "-" + O + "-" + L;
return strDate
}
// 微信相干操纵
function r() { // weixin api
WeixinJSBridge.invoke("getInstallState", {
packageName: "com.jingdong.app.mall",
packageUrl: "openApp.jdMobile://"
}, function(M) {
var N = M.err_msg,
L = 0;
if (N.indexOf("get_install_state:yes") > -1) {
j = true
}
})
}
// 依据是不是存在js框架举行dom和时候的绑定
function bind(dom, event, fun) { // bind event
if (Query) {
selector("#" + dom).bind(event, fun)
} else {
selector("#" + dom).addEventListener(event, fun, !1)
}
}

function z(L) {
var M = (L || "mGen") + (++i);
return M
}
// 微信操纵
if (ifWeixin) { // if navigitor is weixin
if (window.WeixinJSBridge && WeixinJSBridge.invoke) {
r()
} else {
document.addEventListener("WeixinJSBridgeReady", r, !1)
}
} // 假如存在js框架
if (Query) {
selector = window.$;
Hquery = window.$
} else {
selector = function(obj) {
if (typeof obj == "object") {
return obj
}
return document.querySelector(obj);
};
if (!window.$) {
window.$ = Hquery = selector
} else {
Hquery = window.$
}
}
window.Onblur= function() {
for (var L = 0; L clearTimeout(g[L])
}
};
// 设置COOKIE。
function e(N) {
var M = document.COOKIE.indexOf(N + "=");
if (M == -1) {
return ""
}
M = M + N.length + 1;
var L = document.COOKIE.indexOf(";", M);
if (L == -1) {
L = document.COOKIE.length
}
return document.COOKIE.substring(M, L)
}
// 设置COOKIE
function l(N, P, L, Q, O) {
var R = N + "=" + escape(P);
if (L != "") {
var M = new Date();
M.setTime(M.getTime() + L * 24 * 3600 * 1000);
R += ";expires=" + M.toGMTString()
}
if (Q != "") {
R += ";path=" + Q
}
if (O != "") {
R += ";domain=" + O
}
document.COOKIE = R
}

// 翻开的链接鸠合
function F(L) {
var url = {
downAppURl: "http://h5.m.jd.com/active/download/download.html?channel=jd-m",
downAppIos: "http://union.m.jd.com/download/go.action?to=http%3A%2F%2Fitunes.apple.com%2Fcn%2Fapp%2Fid414245413&client=apple&uniOnId=12532&subuniOnId=m-top&key=e4dd45c0f480d8a08c4621b4fff5de74",
downWeixin: "http://a.app.qq.com/o/simple.jsp?pkgname=com.jingdong.app.mall&g_f=991850",
downIpad: "https://itunes.apple.com/cn/app/jing-dong-hd/id434374726?mt=8",
inteneUrl: "openApp.jdMobile://360buy?type=1",
inteneUrlParams: null,
openAppBtnId: "",
closePanelBtnId: "",
closePanelId: "",
closeCallblack: null,
closeCallblackSource: null,
COOKIEFlag: null,
noRecord: false,
sourceType: "JSHOP_SOURCE_TYPE",
sourceValue: "JSHOP_SOURCE_VALUE",
openAppEventId: "MDownLoadFloat_OpenNow",
closePanelEventId: "MDownLoadFloat_Close"
};
if (L) {
for (var M in L) {
if (M && L[M]) {
url[M] = L[M]
}
}
}
return url
}
// 敲黑板 重点内容。看京东是怎样处理兼容题目的。
function openApp(N, L) { // openApp
var R = h(N); //猎取相对应的url
var O = null;
if (ifWeixin) { // 假如是微信端
var M = null;
if (j) {
M = R
} else {
M = N.downWeixin
}
location.href = M; // 直接运用location.href翻开
return
}
if (ifiPad) { // 假如是ipad
O = N.downIpad
} else {
if (ifiPhone) { // 假如是iphone
O = N.downAppIos
} else {
O = N.downAppURl
}
}

if (ifChrome) { // 假如是chrome
if (ifAndroid) { //安卓浏览器
var Q = R;
R = y(Q);
// 延后50毫秒
setTimeout(function() {
window.location.href = R
}, 50)
}
}
if (ifSafari && version >= 9) { // 推断safari版本 假如大于9
setTimeout(function() { // 必需要运用settimeout
var S = document.createElement("a"); //建立a元素
S.setAttribute("href", R), S.style.display = "none", document.body.appendChild(S);
var T = document.createEvent("HTMLEvents"); // 返回新建立的 Event 对象,具有指定的范例。
T.initEvent("click", !1, !1)// 初始化新事宜对象的属性, S.dispatchEvent(T) // 绑定事宜
}, 0)
} else {
document.querySelector("#" + iframe).src = R // 将iframe增添src
}
var P = Date.now();
setTimeout(function() {
if (L) {
var S = setTimeout(function() {
x(P, O)
}, 1500);
g.push(S)
}
}, 100)
}
// x要领
function x(N, downUrl) {
var L = Date.now();
if (N && (L - N) <(1500 + 200)) {
window.location.href = downUrl
}
}

function h(N) {
var V = [];
var P = N.inteneUrlParams;
var T = {
category: "jump",
des: "productDetail"
};
if (N.sourceType && N.sourceValue) {
T.sourceType = N.sourceType;
T.sourceValue = N.sourceValue;
if (P && !P.sourceType && !P.sourceValue) {
P.sourceType = N.sourceType;
P.sourceValue = N.sourceValue
}
}
if (P) {
for (var U in P) {
if (U && P[U]) {
V.push('"' + U + '":"' + P[U] + '"')
}
}
} else {
for (var U in T) {
if (U && T[U]) {
V.push('"' + U + '":"' + T[U] + '"')
}
}
}
try {
var Q = MPing.EventSeries.getSeries();
if (Q) {
var W = JSON.parse(Q);
W.jdv = encodeURIComponent(e("__jdv"));
W.unpl = encodeURIComponent(e("unpl"));
W.mt_xid = encodeURIComponent(e("mt_xid"));
W.mt_subsite = encodeURIComponent(e("mt_subsite"))
}
var S = {
mt_subsite: encodeURIComponent(e("mt_subsite")),
__jdv: encodeURIComponent(e("__jdv")),
unpl: encodeURIComponent(e("unpl")),
__jda: encodeURIComponent(e("__jda"))
};
Q = JSON.stringify(W);
V.push('"m_param":' + Q);
V.push('"SE":' + JSON.stringify(S))
} catch (R) {
V.push('"m_param":null')
}
var M = "{" + V.join(",") + "}";
var O = N.inteneUrl.split("?");
var L = null;
if (O.length == 2) {
L = O[0] + "?" + O[1] + "¶ms=" + M
} else {
L = O[0] + "?params=" + M
}
return L
} function y(L) {
return "intent://m.jd.com/#Intent;scheme=" + L + ";package=com.jingdong.app.mall;end"
} function n(L) {
if (L.openAppBtnId) {
B[L.openAppBtnId] = L;
G(L.openAppBtnId, L.openAppEventId);
bind(L.openAppBtnId, "click", function() {
var P = this.getAttribute("id");
var M = B[P];
if (!t) {
var N = document.createElement("iframe");
N.id = iframe;
document.body.appendChild(N);
document.getElementById(iframe).style.display = "none";
document.getElementById(iframe).style.width = "0px";
document.getElementById(iframe).style.height = "0px";
t = true
}
var O = M.COOKIEFlag ? "downloadAppPlugIn_downCloseDate_" + M.COOKIEFlag : "downloadAppPlugIn_downCloseDate";
l(O, Date.now() + "_2592000000", 60, "/", "m.jd.com");
l(O, Date.now() + "_2592000000", 60, "/", "m.jd.hk");
openApp(M, true)
})
}
} function D(M) {
if (M.closePanelBtnId && M.closePanelId) {
B[M.closePanelBtnId] = M;
G(M.closePanelBtnId, M.closePanelEventId);
var Q = M.COOKIEFlag ? "downloadAppPlugIn_downCloseDate_" + M.COOKIEFlag : "downloadAppPlugIn_downCloseDate";
var O = e(Q);
var P = null;
if (O) {
P = O.split("_");
if (P.length == 2) {
P[0] = parseInt(P[0], 10);
P[1] = parseInt(P[1], 10)
} else {
P = null
}
}
var L = Date.now();
if (Html5Plus() || (!M.noRecord && P && P.length == 2 && (L - P[0]) document.querySelector("#" + M.closePanelId).style.display = "none";
if (M.closeCallblack) {
var N = M.closeCallblackSource ? M.closeCallblackSource : null;
M.closeCallblack.call(N)
}
return
} else {
document.querySelector("#" + M.closePanelId).style.display = "block"
}
bind(M.closePanelBtnId, "click", function() {
var U = this.getAttribute("id");
var R = B[U];
var T = R.COOKIEFlag ? "downloadAppPlugIn_downCloseDate_" + R.COOKIEFlag : "downloadAppPlugIn_downCloseDate";
if (!R.noRecord) {
l(T, Date.now() + "_259200000", 60, "/", "m.jd.com");
l(T, Date.now() + "_259200000", 60, "/", "m.jd.hk")
}
document.querySelector("#" + R.closePanelId).style.display = "none";
if (R.closeCallblack) {
var S = R.closeCallblackSource ? R.closeCallblackSource : null;
R.closeCallblack.call(S)
}
})
}
} function Html5Plus() { // htmlplus
if (Navigator.indexOf("Html5Plus") >= 0) {
return true
} else {
return false
}
} function G(P, M) {
try {
var O = document.getElementById(P);
var L = O.className;
if (L) {
L = L + " J_ping"
} else {
L = "J_ping"
}
O.className = L;
O.setAttribute("report-eventid", M)
} catch (N) {}
} function C(L) {
var M = F(L);
n(M);
D(M)
}
Hquery.downloadAppPlugIn = C;
Hquery.downloadAppPlugInOpenApp = function(L) {
var M = F(L);
openApp(M);
}
});

推荐阅读
author-avatar
万宝盛华猎头
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有