<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)