javascript 点击按钮跳转到app下载页面

javascript 点击html5页面按钮或者单页面的html5页面按钮跳转,当手机上安装app时,点击按钮直接跳转到app里面的链接页面,假如手机中没有安装指定的app,那就要跳转到app下载平台中去下载app。下载app后然后在跳转到点击按钮跳转的页面。

var downloadLink = "http://a.app.qq.com/o/simple.jsp?pkgname=com.clcw.clcwapp",
downloadTimer;
function openAppTip(openLink) {
var htmlStr = '<div class="download-tip">'
+'<div class="fl">'
+'<span class="img"></span>'
+'<div>开新用车</div>'
+'<p>专业车主服务平台</p>'
+'</div>'
+'<div class="fr">'
+'<a href="#">立即打开</a>'
+'</div>'
+'</div>';
$("body").prepend(htmlStr);
$(".download-tip div.fr a").on("click", function(e) {
openApp(openLink, true);
});
}

//实际上就是新建一个iframe的生成器
var createIframe=(function(){
var iframe;
return function(){
if(iframe){
return iframe;
}else{
iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
return iframe;
}
}
})();

function openApp(openLink, isShare){
var localUrl=openLink;
var openIframe=createIframe();
var userAgent = navigator.userAgent;
// ios 处理
if( userAgent.indexOf('iPhone') > -1 || userAgent.indexOf('iPad') > -1 ){
if(isGreaterThan9()){
location.href = localUrl.replace("clcwapp:", "https:");//实际上不少产品会选择一开始将链接写入到用户需要点击的a标签里
return;
}
window.location.href = localUrl;

if (isShare) {
downloadTimer = setTimeout(function () {
window.location.href = downloadLink;
}, 2000);

$(document).one('visibilitychange webkitvisibilitychange', function() {
var tag = document.hidden || document.webkitHidden;
if (tag) {
clearTimeout(downloadTimer);
}
})

$(window).one('pagehide', function() {
clearTimeout(downloadTimer);
})
}

// android 处理
}else if( userAgent.indexOf('Android') > -1 ){
// 微信处理
if ( userAgent.indexOf('MicroMessenger') > -1 ) {
showWechatOpenTip(downloadLink);
return;
}

if (isChrome()) {
//chrome浏览器用iframe打不开得直接去打开,算一个坑
window.location.href = localUrl;
// openIframe.src = localUrl;
} else {
//抛出你的scheme
openIframe.src = localUrl;
}
if (isShare) {
downloadTimer = setTimeout(function () {
window.location.href = downloadLink;
}, 2000);

$(document).one('visibilitychange webkitvisibilitychange', function() {
var tag = document.hidden || document.webkitHidden;
if (tag) {
clearTimeout(downloadTimer);
}
})

$(window).one('pagehide', function() {
clearTimeout(downloadTimer);
})
}
}else{

//主要是给winphone的用户准备的,实际都没测过,现在winphone不好找啊
openIframe.src = localUrl;
if (isShare) {
downloadTimer = setTimeout(function () {
window.location.href = downloadLink;
}, 2000);

$(document).one('visibilitychange webkitvisibilitychange', function() {
var tag = document.hidden || document.webkitHidden;
if (tag) {
clearTimeout(downloadTimer);
}
})

$(window).one('pagehide', function() {
clearTimeout(downloadTimer);
})
}
}
}

function openApp2(openLink, isShare){
var localUrl = openLink,
userAgent = navigator.userAgent;
alert(userAgent)
// ios 处理
if( userAgent.indexOf('iPhone') > -1 || userAgent.indexOf('iPad') > -1 ){
if(isGreaterThan9()){
// alert(1);
location.href = localUrl.replace("clcwapp:", "https:");//实际上不少产品会选择一开始将链接写入到用户需要点击的a标签里
// window.location.href = downloadLink;
} else {
window.location.href = localUrl;
}
if (isShare) {
setTimeout(function () {
window.location.href = downloadLink;
}, 2000);
}

// android 处理
}else if( userAgent.indexOf('Android') > -1 ){
// 微信处理
if ( userAgent.indexOf('MicroMessenger') > -1 ) {
showWechatOpenTip(downloadLink);
return;
}
window.location.href = localUrl;
if (isShare) {
var downloadTimer = setTimeout(function () {
window.location.href = downloadLink;
}, 2000);

$(document).one('visibilitychange webkitvisibilitychange', function() {
var tag = document.hidden || document.webkitHidden;
if (tag) {
clearTimeout(downloadTimer);
}
})

$(window).one('pagehide', function() {
clearTimeout(downloadTimer);
})
}

// winphone 处理
}else{
window.location.href = localUrl;
if (isShare) {
setTimeout(function () {
window.location.href = downloadLink;
}, 2000);
}
}
}

function isGreaterThan9() {
var ua = navigator.userAgent.toLowerCase();
var tempArr = ua.match(/iphone os ([0-9]+)_/) || ua.match(/cpu os ([0-9]+)_/);
if (tempArr) {
return parseInt( tempArr[1] ) >= 9;
}
}

function isChrome(arguments) {
return navigator.userAgent.indexOf("Chrome") > -1;
}

function showWechatOpenTip(downloadLink) {
var htmlStr = '<div class="wechat-openapp">'
+'<div>'
+'<span class="img"></span>'
+'<p>前端菜鸟</p>'
+'<p>请点击右上角用浏览器打开</p>'
+'<span class="arrow-tip"></span>'
+'</div>'
+'<div>'
+'<p>未安装开新用车</p>'
+'<span class="img"></span>'
+'<p style="color: #3d3d3d;">专业的车主服务平台</p>'
+'<a href="'+downloadLink+'">立即下载</a>'
+'</div>'
+'</div>';

$("body").prepend(htmlStr);
}

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: