[英]Possible to resize iFrame after loading contents from a different domain?
I'm looking to resize an iFrame on a page once its contents are loaded. 加载内容后,我希望调整页面上iFrame的大小。 Unfortunately the content being loaded in the iFrame are from a different domain. 不幸的是,iFrame中加载的内容来自其他域。 I've tried to use the following code to call a function in the parent window but it's throwing security errors: 我尝试使用以下代码在父窗口中调用函数,但它引发安全错误:
<body onload="parent.document.someFunction(document.body.scrollHeight);">
Is there a reliable way to pull this off using content from other domains (we have access to these domains and can FTP) or do I need to tell the client they have to mirror the content on their own domain? 是否有可靠的方法可以使用其他域中的内容(我们可以访问这些域并且可以使用FTP)来实现这一目标,或者我是否需要告诉客户端他们必须在自己的域中镜像内容?
Please see this post and my answers: Calling a parent window function from an iframe 请参阅这篇文章和我的答案: 从iframe调用父窗口函数
It is possible, but not easy. 有可能,但并不容易。 You have to hack about a bit to get it to work. 您必须花点时间才能使其正常工作。
I believe that, because you have access to the framed domain and frame page domains, you will be able to use the document.domain-in-the-head trick. 我相信,因为您可以访问框架域和框架页面域,所以可以使用document.domain-in-head-head技巧。
<script>
document.domain = "mydomain.com";
</script>
Update: 更新:
Can I just check the following?: 我可以检查以下内容吗:
Have you put the document.domain <script>
in the head of both pages? 您是否将document.domain <script>
放在两页的开头?
Do both have the exact same string, ie both have "subdomain.mydomain.com"
or both have "mydomain.com"
? 两者都具有完全相同的字符串,即都具有"subdomain.mydomain.com"
还是都具有"mydomain.com"
? Typically you use this to ensure that the framed page has the same document domain as the parent. 通常,您使用它来确保框架页面与父页面具有相同的文档域。
Do you still get a security error after calling a parent function from the framed page? 从框架页面调用父函数后,您仍然收到安全错误吗?
If not, this is the function I use to resize my frame. 如果没有,这就是我用来调整框架大小的功能。 It is located in the head of the framed page. 它位于框架页面的顶部。
window.ResizeFrame = function (newHeight) { if (window.parent && window.parent.document) { var $frame = $(window.parent.document).find("#frame-id"); if ($frame.length) { if (typeof (newHeight) === "number") { $frame.css("height", newHeight); } } } };
Let me know how it goes! 让我知道事情的后续!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.