(function(scope, wakeup) {
if (!scope.wakeup) {scope.wakeup = wakeup();}//这一行会在打包时修改成不同版本,修改时请谨慎
})(window, function() {
var h5_uid = document.cookie.match(/(?:^|\s)cna=([^;]+)(?:;|$)/),
load_chain = [];
if (h5_uid) {
h5_uid = h5_uid[1];
}
function url2obj(url) {
var re = {},
unUrl,
params;
if (url.length && url.substr(0, 5) !== 'data:') {
//将query前后分开
unUrl = url.split('?');
//无query或?有多个
if (unUrl.length == 1) {
unUrl = url.split('#');
if (unUrl.length >= 2) {
unUrl = [unUrl[0], '#' + unUrl.slice(1).join('#')];
} else {
unUrl = [unUrl[0], ''];
}
} else if (unUrl.length > 2) {
unUrl = [unUrl[0], unUrl.slice(1).join('?')];
}
//尝试处理protocol
unUrl[0] = unUrl[0].split('//');
if (unUrl[0].length !== 2) {
//与预期不符的统一处理
unUrl[0] = [undefined, unUrl[0].join('//')];
}
if (unUrl[0][0] !== undefined) {
re.protocol = unUrl[0][0];
}
re.path = unUrl[0][1];
//取query部分
//先把hash处理掉
params = unUrl[1].split('#');
if (params.length >= 2) {
re.hash = params.slice(-1)[0];
params = params.slice(0, -1).join('#');
} else {
params = params[0];
}
//将query分成键值对
if (params) {
re.params = {};
params = params.split('&');
for (var i = 0, maxi = params.length; i < maxi; i++) {
params[i] = params[i].split('=');
if (params[i][1] !== undefined) {
params[i][1] = params[i].slice(1).join('=');
try {
params[i][1] = decodeURIComponent(params[i][1]);
} catch(e) {
}
}
re.params[params[i][0]] = params[i][1];
}
}
}
return re;
}
function obj2url(obj) {
var re = [],
query = [],
query_added = false;
if (obj.protocol !== undefined) {
re.push(obj.protocol + '//');
}
if (obj.path !== undefined) {
obj.path && re.push(obj.path);
}
if (obj.params !== undefined) {
for (var key in obj.params) {
if (!query_added) {
re.push('?');
query_added = true;
}
if (obj.params.hasOwnProperty(key)) {
if (obj.params[key] !== undefined) {
query.push([key, '=', encodeURIComponent(obj.params[key])].join(''));
} else {
query.push(key);
}
}
}
re.push(query.join('&'));
}
obj.hash && re.push('#' + obj.hash);
return re.join('');
}
function appendParams(url) {
var points = ['ali_trackid', 'refpid', 'pid'],
currentUrl = url2obj(location.href),
params = {};
if (url.params === undefined) {
url.params = {};
}
if (currentUrl.params) {
//透传参数
for (var key in currentUrl.params) {
if (!url.params.hasOwnProperty(key)) {
url.params[key] = currentUrl.params[key];
}
}
//添加point参数
params.from = 'h5';
for (var i = 0, maxi = points.length; i < maxi; i++) {
if (currentUrl.params[points[i]]) {
params[points[i]] = currentUrl.params[points[i]];
}
}
if (h5_uid) {
params.h5_uid = h5_uid;
}
delete currentUrl.params;
delete currentUrl.hash;
params.url = obj2url(currentUrl);
url.params.point = JSON.stringify(params);
}
return url;
}
function useAnchorLink(url, pending) {
var a = document.createElement('a'),
e = new MouseEvent('click');
a.setAttribute('href', url);
a.style.display = 'none';
function add() {
document.body.appendChild(a);
a.dispatchEvent(e);
}
doWhenLoad(add, pending);
}
function callInIframe(url, pending) {
var iframe = document.createElement('iframe');
iframe.id = 'callapp_iframe_' + Date.now();
iframe.frameborder = '0';
iframe.style.cssText = 'display:none;border:0;width:0;height:0;';
function add() {
document.body.appendChild(iframe);
iframe.src = url;
}
doWhenLoad(add, pending);
}
function doWhenLoad(add, pending) {
if (document.readyState == "complete" || document.readyState == "loaded") {
add();
} else {
if (pending !== undefined) {
load_chain.push(add);
} else {
window.addEventListener("load", add);
}
}
}
function goldsend(data) {
var img = new Image(),
id = "_live_" + Math.random(),
target = url2obj('//wgo.mmstat.com/wkp.try.d');
window[id] = img;
img.onload = img.onerror = function() {
window[id] = null;
};
target.params = {
'cache': Math.floor(Math.random() * 268435456).toString(16),
'gmkey': 'EXP',
'gokey': JSON.stringify(data),
'cna': '',
'spm-cnt': '0.0.0.0',
'isbeta': 9,
'logtype': 2
};
img.src = obj2url(target);
img = null;
}
function normal_encode(obj, scheme) {
obj.protocol = scheme;
return obj2url(obj);
}
function tbopen_encode(obj) {
var tbopen = 'tbopen://m.taobao.com/tbopen/index.html?action=ali.open.nav&module=h5&bootImage=0&h5Url=';
return tbopen + encodeURIComponent(obj2url(obj));
}
function wakeup(url, options) {
var target = url2obj(url),
ua = navigator.userAgent,
isiOS = /iPhone|iPad|iPod/i.test(ua),
isIE = /Trident/i.test(ua),
iOS9SafariFix,
AndroidBCFix,
iOS_version;
if (isIE) {
return;
}
options = options || {};
goldsend({
h5_uid: h5_uid || '',
url: url,
ua: navigator.userAgent,
to: options.scheme || 'aliapp_tb',
v: '0.1.13'
});
appendParams(target);
//iOS >= 9.0且是Safari,需要用a标签
if (isiOS && /Safari/i.test(ua)) {
iOS_version = ua.match(/Version\/([\d\.]+)/i);
if (iOS_version && iOS_version.length == 2) {
iOS_version = +iOS_version[1];
if (iOS_version >= 9) {
iOS9SafariFix = true;
}
}
}
//百川需要用tbopen
if (!isiOS && /AliApp\(BC\/\d+/i.test(ua)) {
AndroidBCFix = true;
}
if (iOS9SafariFix) {
/**
* iOS9以上的Safari里,使用iframe打开url scheme没有任何反应
*/
if (options.scheme === 'tbopen') {
target = tbopen_encode(target);
} else {
target = normal_encode(target, options.scheme || 'taobao:');
}
useAnchorLink(target, options.toload);
} else if (AndroidBCFix && options.scheme === undefined) {
callInIframe(tbopen_encode(target), options.toload);
} else {
/**
* 在如网易的webview里,iframe的生成需要等待DOM的load事件。其表现和Safari中的非常类似,怀疑是WebKitWebView。
*/
if (options.scheme === 'tbopen') {
target = tbopen_encode(target);
} else {
target = normal_encode(target, options.scheme || 'taobao:');
}
callInIframe(target, options.toload);
}
}
wakeup.loaded = function() {
while(load_chain.length) {
try {
load_chain.shift()();
} catch (e) {}
}
};
return wakeup;
});
上面是淘宝自己唤起手机淘宝的APP的js代码
但是好复杂哦
希望那位大神帮忙优化下js 能直接唤起https://item.taobao.com/item.htm?id=539608081526这样的链接即可
本来用Iframe唤起 但是ios9以上版本都不支持唤起, 麻烦大神精简个能唤起的代码给小弟 小弟对JS实在不擅长
大神最好能帮我把上面的代码详细注释下 我也理解下这个JS的作用
2 个解决方案
#1
location.href='https://item.taobao.com/item.htm?id=539608081526'
#2
你这样多任何都没有判断 而且淘宝是要taobao://唤起的 可能你不理解淘宝的唤起 也没看到js
<script>
function openApp(url) {
var ua = navigator.userAgent.toLowerCase();
var tb = url.replace("http://", "").replace("https://", "");
if( ua.match(/iphone os 9/i) == "iphone os 9" ) {
window.location = "taobao://" + tb;
window.setTimeout(function() {window.location = url;},4000);
}else{
var ifr = document.createElement('iframe');
ifr.src = 'taobao://' + tb;
ifr.style.display = 'none';
document.body.appendChild(ifr);
window.location = url;}}
//只在有优惠券的时候执行
openApp('https://item.taobao.com/item.htm?id=539608081526');
</script>
这个判断是ios 9系统 我需要判断 ios 9以上的 包括ios 10系统等等都支持唤起
#1
location.href='https://item.taobao.com/item.htm?id=539608081526'
#2
你这样多任何都没有判断 而且淘宝是要taobao://唤起的 可能你不理解淘宝的唤起 也没看到js
<script>
function openApp(url) {
var ua = navigator.userAgent.toLowerCase();
var tb = url.replace("http://", "").replace("https://", "");
if( ua.match(/iphone os 9/i) == "iphone os 9" ) {
window.location = "taobao://" + tb;
window.setTimeout(function() {window.location = url;},4000);
}else{
var ifr = document.createElement('iframe');
ifr.src = 'taobao://' + tb;
ifr.style.display = 'none';
document.body.appendChild(ifr);
window.location = url;}}
//只在有优惠券的时候执行
openApp('https://item.taobao.com/item.htm?id=539608081526');
</script>
这个判断是ios 9系统 我需要判断 ios 9以上的 包括ios 10系统等等都支持唤起