Javascript 使用Cordova,我如何在Chrome中而不是在应用程序浏览器中打开外部URL?
有人问过这个问题,但所有的解决方案都可以追溯到2013年,而我还没有任何答案可以用于最新版本的PhoneGap Build/Cordova 我有一个这样的链接,我想在Android上的Chrome中打开Javascript 使用Cordova,我如何在Chrome中而不是在应用程序浏览器中打开外部URL?,javascript,android,cordova,phonegap-plugins,phonegap-build,Javascript,Android,Cordova,Phonegap Plugins,Phonegap Build,有人问过这个问题,但所有的解决方案都可以追溯到2013年,而我还没有任何答案可以用于最新版本的PhoneGap Build/Cordova 我有一个这样的链接,我想在Android上的Chrome中打开 <a href="https://twitter.com/humphreybc" target="_blank">Twitter</a> 我曾尝试过使用window.open(url,_system),正如其他答案中所建议的那样,并在appbrowser中包括cordo
<a href="https://twitter.com/humphreybc" target="_blank">Twitter</a>
我曾尝试过使用window.open(url,_system)
,正如其他答案中所建议的那样,并在appbrowser中包括cordova插件
插件,但是:
\u target='blank'
属性的链接处理程序:
$(document).on('click', 'a[target="_blank"]', function (e) {
e.preventDefault();
var url = this.href;
window.open(url,"_system");
});
…但仍会在应用程序浏览器中打开链接。我使用Cordova和JQM,并使用此功能在设备浏览器中打开链接
function open_url( link ) {
var ref = window.open(encodeURI( link), '_system', 'transitionstyle=fliphorizontal');
}
包括encodeURI可能值得一试。。您使用的是什么html框架/库(ionic/JQM other?您是否尝试过类似的东西(在JS代码中):
在我的例子中,我想根据Cordova应用程序中的用户设置使用Chrome或任何系统默认协议处理程序。在iOS上,或者在其他平台上,
\u系统
不能像预期的那样与googlechrome:
(iApp浏览器版本1.1.1和cli Cordova 5.4.1或iOS js Cordova.version=3.9.2)等协议一起工作。基于混合应用程序webview的\u self
目标,\u系统
被传递,其他目标转到另一个webview。我找到了一种使用hidden=yes
隐藏iab窗口的方法,它只是将请求按原样传递给操作系统。因此,这有效地执行了协议的系统调用。在下面的脚本中打开时,应用程序需要像您提到的那样在config.xml中使用适当的allow origin/intent语句,并可能检查或调整协议(例如将http:
更改为googlechrome:
)
$(document).on('click', (function(base){
/*
this single-page hybrid app uses fragment (ie #/go/here/now ) to navigate
so links will always have the same base url eg
file:///path/to/app.html
http://localhost/path/to/app.html
*/
var open;
base = base.location.origin + base.location.pathname;
return function(e){
var a, href;
if(!(a=e.target.closest('a')) || (href=a.href).indexOf(base) === 0 || !href) return;
e.preventDefault();
// assuming cordova is available with the plugin https://github.com/apache/cordova-plugin-inappbrowser
// use with any protocol eg: 'googlechrome://www.google.com/' tel:+18005551212 http://stackoverflow.com
window._external_app_window = (open || (open = ((window.cordova||{}).InAppBrowser||window).open))(
href, '_external_app_window', 'hidden=yes'
);
};
})(this))
.on('resume', function(){
if(window._external_app_window) window._external_app_window.close();
});
你可以试试这个
只需回顾旧问题并将其标记为已回答即可。我最终做了这样的事:
function onDeviceReady() {
return $(document).on('click', 'a[target="_blank"]', function(e) {
e.preventDefault();
return window.open(this.href, '_system');
});
};
if (!!window.cordova) {
document.addEventListener('deviceready', onDeviceReady, false);
}
以下代码适用于我,您可以尝试一次:
"googlechrome://navigate?url=" + url
i.e. window.open("googlechrome://navigate?url=" + url,"_system"); // here you can try with _system or _blank as per your requirement
@PeterScott感谢您的评论,但我不认为这是电话间隙构建问题。另外,这个答案已经有2.5年了,所以现在可能不相关了。你是如何安装这个插件的?该代码应该可以工作。您可以尝试使用
cordova.InAppBrowser.open(url,“_system”)代码>如果我们传递m3u8 url,您的解决方案能否在chrome上播放m3u8?
<a class="item" href="#" onclick="window.open('https://twitter.com/humphreybc', '_system', 'location=yes'); return false;">Open Browser</a>
function onDeviceReady() {
return $(document).on('click', 'a[target="_blank"]', function(e) {
e.preventDefault();
return window.open(this.href, '_system');
});
};
if (!!window.cordova) {
document.addEventListener('deviceready', onDeviceReady, false);
}
"googlechrome://navigate?url=" + url
i.e. window.open("googlechrome://navigate?url=" + url,"_system"); // here you can try with _system or _blank as per your requirement