How can I resize a cross-domain iFrame that I do not have control over

こ雲淡風輕ζ 提交于 2021-02-10 15:39:44

问题


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.

  • Dynamically resize iframe
  • Resizing an iframe based on content
  • Resize iframe height according to content height

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.


回答1:


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.



来源:https://stackoverflow.com/questions/30856767/how-can-i-resize-a-cross-domain-iframe-that-i-do-not-have-control-over

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!