【问题标题】:How do I open a different domain iFrame link with the InAppBrowser in Ionic?如何在 Ionic 中使用 InAppBrowser 打开不同的域 iFrame 链接?
【发布时间】:2016-07-14 05:14:47
【问题描述】:

所以我正在使用 ionic 创建一个应用程序,我遇到的问题是当我将 disqus 作为 iframe 加载时。 iframe 中有链接,例如登录名和指向其他帖子的链接等,但是它们不会在 android 中打开。我制作了一个脚本来使用 InAppBrowser 打开应用程序中的所有链接,它适用于除 iframe 之外的所有内容。

现在我明白了,因为 iframe 来自另一个域,我无法修改它或捕获链接等,所以我的问题是你知道我可以使用 InAppBrowser 打开 iframe 中的链接的任何其他方式吗?

编辑,澄清我不希望 iframe 内的链接在其中打开,但我希望 InAppBrowser 在新窗口中打开它们。

【问题讨论】:

  • 您将无法激活 iframe 中页面的链接,但您可以尝试访问 iframe 元素并将其 src 更改为您需要导航的页面的 url,例如登录页面、链接页面等的url。
  • 我只希望在父级的新窗口中使用 inappbrowser 打开链接,而不是在链接所在的 iframe 内部

标签: android cordova iframe ionic-framework


【解决方案1】:

我假设 Cordova InAppBrowser 插件已安装。

试试下面的代码:

<button class="button button-outline button-royal" ng-click="gotoLink('http://www.google.com')">Open Google</button>

在控制器文件中:

$scope.gotoLink = function(url) {
  $window.open(url, '_system', 'location=yes' );
}

你也可以看到这个tutorial for more information

【讨论】:

  • 使用运行良好的 inappbrowser 打开链接没有任何问题。我遇到的问题是打开从另一个域加载的 iframe 内部的链接。
【解决方案2】:

我发现实现这一点的唯一方法是从父窗口实现一个函数到加载的 iframe 中,并在您单击某些内容或打开该链接的任何内容时在该 iframe 中使用此函数。例如,您可以声明一个函数(假设您使用的是 inappbrowser 插件),例如:

function openLink(url, target, options) {
    window.open(url, target, options);
}

然后,将该函数放入您的 iframe(frame 是您的 iframe):

frame.onload = function() {
    this.contentWindow.launchIAB = openLink;
}

最后,在您的 iframe 代码中随时使用它:

document.querySelector('a.openLink').on('click', function() {launchIAB(this.href);});

【讨论】:

  • 对不起,我是 iframe 的新手,但我理解它的方式是因为我从另一个域加载 iframe,我无法访问代码,所以我将如何在 iframe 中使用它?
猜你喜欢
  • 1970-01-01
  • 2014-07-20
  • 1970-01-01
  • 1970-01-01
  • 2013-07-22
  • 2012-02-27
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
相关资源
最近更新 更多