Cordova 如何通过iframe中的链接打开新窗口
我正在编写一个cordova应用程序,它应该适用于android、ios和web 在那个应用程序中,我使用了一个iframe,在这里我加载html页面(从我们的服务器,如果需要的话,我可以操纵它)。 因此,我目前正在使用iframe,如:Cordova 如何通过iframe中的链接打开新窗口,cordova,iframe,anchor,Cordova,Iframe,Anchor,我正在编写一个cordova应用程序,它应该适用于android、ios和web 在那个应用程序中,我使用了一个iframe,在这里我加载html页面(从我们的服务器,如果需要的话,我可以操纵它)。 因此,我目前正在使用iframe,如: <iframe sandbox="allow-scripts allow-same-origin allow-top-navigation" name="iframe" frameBorder="0" seamless="seamless" src="h
<iframe sandbox="allow-scripts allow-same-origin allow-top-navigation" name="iframe" frameBorder="0" seamless="seamless" src="http://www.ourserver.com/file.html" />
在这些html文件中,我有一些链接,我想在新窗口中打开这些链接。我编写了一个函数来处理链接的打开,可以从iframe外部通过window.openLink(url)
访问该链接。
知道如何从iframe中打开链接吗?您可以有两种可能性:
$(document).ready(function () {
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.open = cordova.InAppBrowser.open;
}
$(document).on('click', 'a[href^=http], a[href^=https]', function (e) {
e.preventDefault();
var $this = $(this),
target = $this.data('inAppBrowser') || '_system'; // system open the device browser. _blank open inappbrowser
window.open($this.attr('href'), target, 'location=no');
});
});
我知道这个问题已经过时了,但因为我面临着类似的问题,我还是要补充一个答案 我们的问题是,我们无法控制正在呈现的iframe,因此无法进行必要的覆盖以使Zappescu的解决方案工作,因此我们最终扩展Cordova以处理请求以不同方式在主框架以外的其他位置打开的链接,并通过平台浏览器打开它们 我们的解决方案目前只支持使用
WKWebView
的iOS,但其他平台应该有类似的解决方案
我们所做的主要工作是将自己的wkuidegate
实例发送到WKWebView
[self.webViewEngine updateWithInfo:@{
kCDVWebViewEngineWKUIDelegate : uiDelegate
}];
在该委托中,我们添加了一个createWebViewWithConfiguration
来处理这些URL
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
if (!navigationAction.targetFrame.isMainFrame) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:navigationAction.request.URL.absoluteString]];
}
return nil;
}
实际的插件如下所示:
希望这能成为其他面对这一问题的人的切入点