Seb*_*ber 10 phonegap-plugins cordova inappbrowser ionic
我有一个Cordova应用程序,它是一个带有单个HTML文件的单页面应用程序.
所有链接都应在系统浏览器中打开.我不想要"嵌入式"InAppBrowser,但实际上是原生系统/外部浏览器.
我们可以在任何地方找到使用InAppBrowser的代码示例:
window.open('http://apache.org', '_system');
Run Code Online (Sandbox Code Playgroud)
但是,为什么我们需要安装InAppBrowser,即使我们甚至不打算使用嵌入式浏览器?
关于链接的目标,有人真的可以实现WebView的行为吗?目前尚不清楚它应该用a target=_blank做什么,但除了打开一个新的浏览器窗口之外我什么都看不到.
请注意,问题似乎只出现在iOS上,因为使用Android(使用Crosswalk插件)target=_blank似乎始终可以正常工作并在新的本机浏览器窗口中打开.
Seb*_*ber 12
所以我用我发现的东西回答了我自己的问题.注意我只在Cordova 5.1.1上处理iOS和Android(使用Crosswalk插件),它可能不适用于其他平台/版本.
即使您不需要嵌入式浏览器,也需要InAppBrowser插件.这使得_system目标可用,触发本机插件代码以打开系统/外部浏览器.
因此,插件似乎是一个"二合一"插件:它允许使用嵌入式浏览器+它允许安全地强制外部系统浏览器打开.
目前尚不清楚默认的WebView行为应该与_blank链接相关(也不是以任何方式对WebView进行标准化)但是我发现没有这个插件或本机代码就无法在iOS上打开外部浏览器.
_self在WebView和_blank本机浏览器中打开如果像我一样你不关心嵌入式浏览器,只想_blank在现有应用程序中打开本地外部浏览器的所有目标,而不会太痛苦(特别是如果应用程序也是一个移动网站...),你可以在应用开始时运行以下代码:
function openAllLinksWithBlankTargetInSystemBrowser() {
if ( typeof cordova === "undefined" || !cordova.InAppBrowser ) {
throw new Error("You are trying to run this code for a non-cordova project, " +
"or did not install the cordova InAppBrowser plugin");
}
// Currently (for retrocompatibility reasons) the plugin automagically wrap window.open
// We don't want the plugin to always be run: we want to call it explicitly when needed
// See https://issues.apache.org/jira/browse/CB-9573
delete window.open; // scary, but it just sets back to the default window.open behavior
var windowOpen = window.open; // Yes it is not deleted !
// Note it does not take a target!
var systemOpen = function(url, options) {
// Do not use window.open becaus the InAppBrowser open will not proxy window.open
// in the future versions of the plugin (see doc) so it is safer to call InAppBrowser.open directly
cordova.InAppBrowser.open(url,"_system",options);
};
// Handle direct calls like window.open("url","_blank")
window.open = function(url,target,options) {
if ( target == "_blank" ) systemOpen(url,options);
else windowOpen(url,target,options);
};
// Handle html links like <a href="url" target="_blank">
// See https://issues.apache.org/jira/browse/CB-6747
$(document).on('click', 'a[target=_blank]', function(event) {
event.preventDefault();
systemOpen($(this).attr('href'));
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12637 次 |
| 最近记录: |