码迷,mamicode.com
首页 > 移动开发 > 详细

HTML5 唤起 APP

时间:2018-02-10 14:57:26      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:listen   add   tun   target   hang   报名   tno   click   ref   

<p><a href="xxx://app/question/95">点击跳转,直接回帖报名</a></p>
 1 /* global navigator, document, window */
 2 
 3 var UA = {
 4     App: navigator.userAgent.indexOf(‘naitangApp‘) >= 0,
 5     WeiXin: navigator.userAgent.indexOf(‘MicroMessenger‘) >= 0,
 6     Android: navigator.userAgent.indexOf(‘Android‘) >= 0,
 7     iPhone: navigator.userAgent.indexOf(‘iPhone‘) >= 0
 8 }
 9 var timeout
10 function replaceSchema(href) {
11     var schema = [
12         [‘xxx://app/question/‘, ‘http://www.xxx.com/question/‘]
13     ]
14     var len = schema.length
15     for (var i = 0; i < len; i += 1) {
16         href = href.replace(schema[i][0], schema[i][1])
17     }
18     return href
19 }
20 document.querySelector(‘body‘).addEventListener(‘click‘, function(e) {
21     var tg = (window.event) ? e.srcElement : e.target
22     while (tg.nodeName.toUpperCase() !== ‘BODY‘) {
23         if (tg && tg.nodeName.toUpperCase() === ‘A‘) {
24             var href = tg.getAttribute(‘href‘)
25             if (href.indexOf(‘naitang://‘) === 0) {
26                 e.preventDefault()
27                 if (UA.App) {
28                     window.location.href = href
29                 } else if (UA.WeiXin) {
30                     window.location.href = ‘http://a.app.qq.com/o/simple.jsp?pkgname=com.xxxxxx‘
31                 } else if (UA.Android) {
32                     window.location.href = href
33                     timeout = setTimeout(function() {
34                         window.location.href = ‘http://a.app.qq.com/o/simple.jsp?pkgname=com.xxxxxx‘
35                     }, 1000)
36                 } else if (UA.iPhone) {
37                     window.location.href = href
38                     timeout = setTimeout(function() {
39                         window.location.href = ‘itms-apps://itunes.apple.com/cn/app/nai-tang/id00000000?mt=8‘
40                     }, 1000)
41                 } else {
42                     var pchref = tg.getAttribute(‘data-pc‘)
43                     href = pchref ? pchref : replaceSchema(href)
44                     window.location.href = href
45                 }
46             }
47             break
48         } else {
49             tg = tg.parentNode
50         }
51     }
52 }, false)
53 function onVisibilityChanged() {
54     var hidden = document.hidden || document.webkitHidden
55     if (hidden && timeout) {
56         window.clearTimeout(timeout)
57     }
58 }
59 document.addEventListener(‘visibilitychange‘, onVisibilityChanged, false)

 

HTML5 唤起 APP

标签:listen   add   tun   target   hang   报名   tno   click   ref   

原文地址:https://www.cnblogs.com/e-cat/p/8438560.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!