cordova- window.open() 即使在添加 inappbrowser pugin 后也无法正常工作



我正在使用cordova开发一个混合移动应用程序。当我点击一个按钮时,我需要在网站中打开一个网址。为此,我正在使用window.open它在浏览器中运行良好,但即使在添加inappbrowser插件后也无法在移动应用程序中工作。

下面我提供我正在运行的代码:

<button type="submit" class="btn btn-primary btn-sm" id="myButton">Get Free Trial</button>`
document.getElementById("myButton").onclick = function () {
window.open("https://indirect-tax.com",'_system');
};
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.open = cordova.InAppBrowser.open;
}

我们全局重定向a标签的指令解决方案,我们使用了与您相同的插件,没有重新定义window.open。您可以修改它以添加规则,此版本仅重定向以"http"开头href链接,并且还评估最终将放置在ngClick属性中的代码(为了安全起见,您应该将其删除,这是一个测试版本(。

angular
.module('yourModule') 
.directive('a', function () {
return {
restrict: 'E',
link: function (scope, element, attrs) {
if ( !attrs.href ){
return;
}
var url = attrs.href;
if ( url.lastIndexOf('http',0) === 0 ){
element.on('click',function(e){
e.preventDefault();
if(attrs.ngClick){
scope.$eval(attrs.ngClick);
}
window.open(encodeURI(url), '_system');
});
};
}
};
});

最新更新