2016-06-09 130 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

希望這可以作爲其他人誰面臨這個問題的切入點

相關問題