如何调整我无法控制的跨域iFrame的大小

时间:2022-08-23 10:16:06

There's a plethora of articles and questions on how to resize iFrames to fit the inner content for URL's that aren't cross-domain, or that are cross-domain but that are under the developers control.

关于如何调整iFrame的大小以适应不是跨域的URL的内部内容,或者是跨域但是在开发人员控制之下的文章和问题有很多。

But I cannot find how to resize an iFrame for which I have zero control over, and thus can't postMessage back to the parent.

但我找不到如何调整我无法控制的iFrame的大小,因此无法将postMessage返回给父级。

Is this possible?

这可能吗?

i.e. Imagine my site mysite.com is trying to iFrame CNN.com. How could I resize the CNN iFrame so that the entire contents of the iFrame displays, without needing to scroll through the CNN iFrame.

想象一下,我的网站mysite.com正在尝试iFrame CNN.com。我怎样才能调整CNN iFrame的大小,以便显示iFrame的全部内容,而无需滚动CNN iFrame。

Edit

编辑

Since the iFrame is cross-domain, the simple solution of reading the content of the iFrame and re-sizing it based on the content height cannot work, due to security restrictions. For example, Chrome gives me this exception when trying to read the content of an iFrame on a different domain:

由于iFrame是跨域的,因此,由于安全限制,读取iFrame内容并根据内容高度重新调整大小的简单解决方案无效。例如,在尝试阅读其他域上的iFrame内容时,Chrome会向我提供此异常:

Uncaught SecurityError: Failed to read the 'contentDocument' property from > 'HTMLIFrameElement': Blocked a frame with origin "http://dev" from accessing a frame with origin "http://cnn.com". Protocols, domains, and ports must match.

未捕获的SecurityError:无法从>'HTMLIFrameElement'读取'contentDocument'属性:阻止具有原点“http:// dev”的帧访问具有原点“http://cnn.com”的帧。协议,域和端口必须匹配。

Due to this, a common solution is to make the iFramed content communicate with the hosting page, as seen here. However, since the URL in my case, CNN.com, is completely 3rd party and out of my control, I cannot follow the cross-domain workaround.

因此,常见的解决方案是使iFramed内容与托管页面进行通信,如此处所示。但是,由于我的案例中的URL CNN.com完全是第三方并且不受我控制,因此我无法遵循跨域解决方法。

1 个解决方案

#1


0  

Based on the comments I received, it seems safe to say that it is impossible to re-size a cross-domain iFrame from the client/browser side.

根据我收到的评论,似乎可以肯定地说,无法从客户端/浏览器端重新调整跨域iFrame的大小。

Perhaps some complex server-side algorithm could request the page and attempt to give hints about the size, but nothing can be done on the client-side.

也许一些复杂的服务器端算法可以请求页面并尝试给出关于大小的提示,但是在客户端无法做任何事情。

#1


0  

Based on the comments I received, it seems safe to say that it is impossible to re-size a cross-domain iFrame from the client/browser side.

根据我收到的评论,似乎可以肯定地说,无法从客户端/浏览器端重新调整跨域iFrame的大小。

Perhaps some complex server-side algorithm could request the page and attempt to give hints about the size, but nothing can be done on the client-side.

也许一些复杂的服务器端算法可以请求页面并尝试给出关于大小的提示,但是在客户端无法做任何事情。