2016-06-09 120 views
1

我正在写一个cordova应用程序,该应用程序适用于android,ios和web。如何通过iframe中的链接打开新窗口

在我使用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文件,我有联系,这是我想在新窗口中打开。我写了一个处理链接打开的函数,可以从iframe之外的window.openLink(url)到达。 任何想法如何从iframe中打开链接?

回答

1

你可以有两种可能性:

  1. 链接在主应用程序窗口中打开,你的应用程序里面, 全屏
  2. 链接在设备浏览器中打开,全屏

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

seems,在实际的时候,你不能打开链接到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'); 
    }); 

}); 
0

我知道这个问题是很过时,但我正面临着类似的问题,我反正加入一个答案。

我们的问题是,我们无法控制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

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

相关问题