【问题标题】:Can a page inside an iframe "see" cross domain if the iframe element itself is visible or not?如果 iframe 元素本身可见或不可见,iframe 内的页面能否“看到”跨域?
【发布时间】:2015-03-05 22:44:50
【问题描述】:

如果我在不同域的 iframe 中加载页面,是否存在包含页面以查看 iframe 元素或其父元素是否可见(例如,使用 jquery 的 is(':hidden'))?

我可以控制这两个域——我可以设置 CORS、属性等等。我尝试的一切仍然给我类似的东西:

Uncaught SecurityError: Blocked a frame with origin "http://s-college.mydomain.com" from accessing a frame with origin "http://qaf.ng.mydomain.com". Protocols, domains, and ports must match.

如果有帮助,基本域实际上是相同的,只是子域不同。

【问题讨论】:

  • 据我所知,没有。
  • 可以使用 postMessage API,因为您可以控制两个域。使用直接访问的另一种方法是将document.domain 设置为两者都匹配,然后您可以使用相同的代码,就好像它是同一域上的 iframe
  • CORS和这种事情没有关系吗?

标签: javascript jquery security iframe cors


【解决方案1】:

如果它们都在同一个高级域中(例如 foo.example.combar.example.com),您可以在 JavaScript 中设置 document.domain

document.domain = 'example.com';

这将允许跨帧通信。

CORS 仅适用于(主要是 AJAX)请求,因此此处不适用。

以防万一其他人发现这个问题不在同一个更高级别的域上:如果他们在不同的域上,您需要使用 window.postMessage 才能跨框架进行通信。

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    相关资源
    最近更新 更多