https://stackoverflow.com/questions/10237031/how-to-open-a-native-ios-app-from-a-web-app var frame = document.createElement('iframe'); frame.src = 'myapp://?params=...'; frame.style.display = 'none'; document.body.appendChild(frame); setTimeout(function() { document.body.removeChild(frame); }, 4); 以上這個方案在喚起app時,當喚起不成功的時候不會有瀏覽器的默認行爲,若是用href跳轉的化就會有瀏覽器的彈窗提示的行爲了
var preventDefault = function(e) { e = e || window.event; if(e.preventDefault) { e.preventDefault(); }else{ e.returnValue = false; } } preventDefault(); 以上的方法試過了,能夠禁止手機上瀏覽器的彈窗的行爲
下面的方法沒試過html
糾結兩天(瀏覽器中喚起本地APP),一直找不到解決方案,今天總算基本搞定。html5
ps:吐槽一下 魔窗那篇文章,爲何就不直接把js代碼開源開源,混淆後的代碼看得我好惱火android
// 判斷瀏覽器 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 ifIos = Navigator.match(/iPhone|iPad|iPd/i) ? true : false; var ifSafari = ifIos && Navigator.match(/Safari/); // ios 設備的版本號 var iosVersion = Navigator.match(/OS\s*(\d+)/) iosVersion = iosVersion ? (iosVersion[1] || 0) : 0; // 安卓版本號 var androidVersion = Navigator.match(/Android\s*(\d+)/) androidVersion = androidVersion ? (androidVersion[1] || 0) : 0;
// 延後50毫秒 setTimeout(function() { location.href = ‘自定義 URL’ }, 50)
setTimeout(function() { // 必需要使用settimeout var a = document.createElement("a"); //建立a元素 a.setAttribute("href", ‘自定義 URL’), a.style.display = "none", document.body.appendChild(a); var t = document.createEvent("HTMLEvents"); // 返回新建立的 Event 對象,具備指定的類型。 t.initEvent("click", !1, !1) // 初始化新事件對象的屬性 a.dispatchEvent(t) // 綁定事件 }, 0)
document.querySelector("#" + iframe).src = ‘自定義 URL’ // 將iframe增長src
var checkOpen = function (cb){ var _clickTime = +(new Date()); function check(elsTime) { if ( elsTime > 3000 || document.hidden || document.webkitHidden) { cb(1); } else { cb(0); } } //啓動間隔20ms運行的定時器,並檢測累計消耗時間是否超過3000ms,超過則結束 var _count = 0, intHandle; intHandle = setInterval(function(){ _count++; var elsTime = +(new Date()) - _clickTime; if (_count>=100 || elsTime > 3000 ) { clearInterval(intHandle); check(elsTime); } }, 20); } checkOpen(function(opened){ // APP沒有打開成功 而且開啓自動跳轉到下載頁 if(opened === 0 && option.autoRedirectToDownloadUrl){ location.href = downloadUrl; } });