简体   繁体   English

调整跨网域iframe的大小

[英]Resizing cross-domain iframe

I've heard that it's possible to poll the url of an iframe for a hash to do something with it from the parent. 我听说,可以轮询iframe的网址以获取哈希值,以便通过父级对其进行处理。 What i need to do is set the height of a cross domain iframe dynamically. 我需要做的是动态设置跨域iframe的高度。 So whenever the height changes, the iframe sets its url to someurl#height. 因此,只要高度发生变化,iframe就会将其网址设置为someurl#height。 Now i need to access the hash (#height) from the parent, but it still won't let me. 现在,我需要从父级访问哈希(#height),但仍然不允许我这样做。 Using a proxy (iframe inside an iframe) is not an option in this case. 在这种情况下,不能使用代理(iframe内的iframe)。 Maybe i'm doing something wrong, how would you poll the url of an iframe? 也许我做错了什么,您将如何查询iframe的网址?

iframe.contentWindow.location.href - security alert iframe.src - returns the url WITHOUT the hash iframe.contentWindow.location.href-安全警报iframe.src-返回没有哈希值的网址

That's not usually the way it's done. 通常这不是完成的方式。 What should be done, is the iframe calls window.parent.location = "#<iframe height>"; iframe调用window.parent.location = "#<iframe height>"; , setting the parent to have the hash value of the iframe height. ,将父级设置为具有iframe高度的哈希值。

The parent page uses either the onhashchange event (IE, Firefox) to capture the change and then set the iframe's height or a timer that checks the hash value every 100ms or so. 父页面使用onhashchange事件(IE,Firefox)捕获更改,然后设置iframe的高度,或者使用计时器每隔100毫秒左右检查一次哈希值。 This is how Google CSE does it, at least. 至少Google CSE就是这样做的。

See also my answer to a similar question: 另请参阅我对类似问题的回答:

Handle URL anchor change event in js 处理js中的URL锚更改事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM