如何调整我无法控制的跨域iFrame的大小 [英] How can I resize a cross-domain iFrame that I do not have control over

查看:51
本文介绍了如何调整我无法控制的跨域iFrame的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何调整iFrame的大小以适合非跨域或跨域但受开发人员控制的URL的内部内容,有很多文章和问题.

但是我找不到如何调整对其零控制的iFrame的大小,因此无法 postMessage 回到父级.

这可能吗?

即想象一下,我的网站mysite.com正在尝试访问iFrame CNN.com.如何调整CNN iFrame的大小,以便显示iFrame的全部内容,而无需滚动浏览CNN iFrame.

修改

由于iFrame是跨域的,由于安全限制,读取iFrame内容并根据内容高度重新调整其大小的简单解决方案不起作用.例如,当尝试在其他域上读取iFrame的内容时,Chrome给了我这个例外:

未捕获的SecurityError:无法从>'HTMLIFrameElement'中读取'contentDocument'属性:阻止访问源为" http://dev 的框架来源为" http://cnn.com "的框架.协议,域,和端口必须匹配.

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

解决方案

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

也许某些复杂的服务器端算法可以请求页面并尝试提供有关大小的提示,但客户端无法执行任何操作.

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.

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.

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.

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:

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.

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.

解决方案

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.

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.

这篇关于如何调整我无法控制的跨域iFrame的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆