使用 Cordova,如何在 Chrome 而不是应用内浏览器中打开外部 URL?
Using Cordova, how can I open external URLs in Chrome, instead of the in-app browser?
有人问过这个问题a few before,但所有的解决方案都可以追溯到 2013 年,我还没有得到任何与最新版本的 PhoneGap Build / Cordova 一起使用的答案。
我有一个像这样的 link,我想在 Android 上的 Chrome 中打开它。
<a href="https://twitter.com/humphreybc" target="_blank">Twitter</a>
在config.xml
中我有以下规则:
<allow-navigation href="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<access origin="*" />
我已尝试按照其他答案中的建议使用 window.open(url, _system)
– 并包含 cordova-plugin-inappbrowser
插件 – 但是:
- 此解决方案似乎不起作用,link只能在应用内浏览器中打开
- 我宁愿简单地使用 target="_blank" 而不是为每个 link
使用 onClick 处理程序
我还按照说明 in this blog 并为 link 添加了一个具有 _target='blank'
属性的处理程序:
$(document).on('click', 'a[target="_blank"]', function (e) {
e.preventDefault();
var url = this.href;
window.open(url,"_system");
});
...但 link 仍会在应用内浏览器中打开。
我将 Cordova 与 JQM 结合使用,并使用此函数处理在设备浏览器中打开 link。
function open_url( link ) {
var ref = window.open(encodeURI( link), '_system', 'transitionstyle=fliphorizontal');
}
也许值得一试,包括 encodeURI ..你在使用什么 html framework/libs(ionic/JQM 其他?)
你有没有尝试过这样的事情(在你的 JS 代码中):
navigator.app.loadUrl(url, { openExternal: true });
在我的例子中,我想使用 Chrome 或基于 Cordova 应用程序中用户设置的任何系统默认协议处理程序。在 iOS 和可能的其他平台上,_system
无法按预期使用 googlechrome:
等协议(inappbrowser 版本 1.1.1 和 cli Cordova 5.4.1 或 [=24 没有任何反应) =] js cordova.version = 3.9.2)。基于 my quick scan of the source _self
目标混合应用程序 webview,_system
被传递,每个其他目标都转到另一个 webview。我找到了一种使用 hidden=yes
隐藏 iab window 的方法,它只是按原样将请求传递给 OS。所以这有效地执行了协议的系统调用。应用程序需要像您提到的那样在 config.xml 中使用适当的允许 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://whosebug.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();
});
you can try this one
<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
有人问过这个问题a few
我有一个像这样的 link,我想在 Android 上的 Chrome 中打开它。
<a href="https://twitter.com/humphreybc" target="_blank">Twitter</a>
在config.xml
中我有以下规则:
<allow-navigation href="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<access origin="*" />
我已尝试按照其他答案中的建议使用 window.open(url, _system)
– 并包含 cordova-plugin-inappbrowser
插件 – 但是:
- 此解决方案似乎不起作用,link只能在应用内浏览器中打开
- 我宁愿简单地使用 target="_blank" 而不是为每个 link 使用 onClick 处理程序
我还按照说明 in this blog 并为 link 添加了一个具有 _target='blank'
属性的处理程序:
$(document).on('click', 'a[target="_blank"]', function (e) {
e.preventDefault();
var url = this.href;
window.open(url,"_system");
});
...但 link 仍会在应用内浏览器中打开。
我将 Cordova 与 JQM 结合使用,并使用此函数处理在设备浏览器中打开 link。
function open_url( link ) {
var ref = window.open(encodeURI( link), '_system', 'transitionstyle=fliphorizontal');
}
也许值得一试,包括 encodeURI ..你在使用什么 html framework/libs(ionic/JQM 其他?)
你有没有尝试过这样的事情(在你的 JS 代码中):
navigator.app.loadUrl(url, { openExternal: true });
在我的例子中,我想使用 Chrome 或基于 Cordova 应用程序中用户设置的任何系统默认协议处理程序。在 iOS 和可能的其他平台上,_system
无法按预期使用 googlechrome:
等协议(inappbrowser 版本 1.1.1 和 cli Cordova 5.4.1 或 [=24 没有任何反应) =] js cordova.version = 3.9.2)。基于 my quick scan of the source _self
目标混合应用程序 webview,_system
被传递,每个其他目标都转到另一个 webview。我找到了一种使用 hidden=yes
隐藏 iab window 的方法,它只是按原样将请求传递给 OS。所以这有效地执行了协议的系统调用。应用程序需要像您提到的那样在 config.xml 中使用适当的允许 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://whosebug.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();
});
you can try this one
<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