如何在 iframe 中通过 link 打开新的 window

How to open a new window by a link within an iframe

我正在编写一个 cordova 应用程序,它应该适用于 android、ios 和网络。

在该应用程序中,我使用 iframe,我正在其中加载 html 页面(来自我们的服务器,如有必要我可以对其进行操作)。 所以我目前正在使用 iframe,例如:

<iframe sandbox="allow-scripts allow-same-origin allow-top-navigation" name="iframe" frameBorder="0" seamless="seamless" src="http://www.ourserver.com/file.html" />

在这些 html 个文件中,我有 link 个文件,我想在一个新的 window 中打开它们。我编写了一个函数来处理 link 的打开,可以从 iframe 外部使用 window.openLink(url) 访问它。 知道如何从 iframe 中打开 link 吗?

你有两种可能:

  1. link 在主应用 window 中打开,在您的应用中, 全屏
  2. link在设备浏览器中打开,全屏

在这两种情况下,您都应该使用 inAppBrowser plugin

seems,在实际情况下,您无法将link打开到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 以处理请求在其他地方打开的链接主框架不同,而是通过平台浏览器打开它们

我们的解决方案目前仅支持iOS使用WKWebView,但其他平台应该有类似的解决方案。

我们做的主要事情是将我们自己的 WKUIDelegate 实例发送到 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;
}

实际插件在这里:https://github.com/trendsales/cordova-iframe-navigation

希望这可以作为面临此问题的其他人的切入点