根据 Iframe 内部的源(这是另一个服务器)来增长 Iframe
我使用 javascript 将 Iframe 扩展到它加载的文档的大小,以删除任何垂直滚动条,如下所示:
function resizeIframes() {
$('iframe').load(function()
{
this.style.height = (this.contentWindow.document.body.offsetHeight + 40) + 'px';
}
);
}
这对于我的使用来说足够好了。但现在我需要使用来自另一台服务器的网站加载 Iframe(实际上是另一个子域,而不是“www.mydomain.com”,而是“services.mydomain.com”),并且根据 Firebug,我不允许从中读取属性通过 Javascript 的其他域。我猜是某种沙盒问题?!
有什么方法可以避免这个问题,或者至少获得一些有关内容大小的信息?我什至愿意检查是否有滚动条并不断增长 iframe 直到它消失,但与 window 对象不同,iframe 对象似乎没有 .scrollbars 属性。
I use javascript to expand an Iframe to the size of the document it loads to remove any vertical scrollbar, like so:
function resizeIframes() {
$('iframe').load(function()
{
this.style.height = (this.contentWindow.document.body.offsetHeight + 40) + 'px';
}
);
}
Which works well enough for my uses. But now I need to load the Iframe with a website from another server (actually another subdomain, instead of "www.mydomain.com" it's "services.mydomain.com") and according to Firebug I'm not allowed to read properties from other domains via Javascript. I'm guessing some kind of sandbox problem?!
Is there any way to circumvent this or at least get some kind of info about the size of the content? I even would be willing to check if there is a scrollbar and continually grow the iframe until it's gone, but unlike the window object an iframe object does not seem to have a .scrollbars property.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您发现此回复不完整(确实如此),我很抱歉。我只是没有带代码示例。我将概述主要概念,您必须自己填写详细信息。
您必须使用一些技巧来做到这一点,因为 XSS 安全性不允许一个站点的框架访问另一个站点的框架:
site1:page1:在站点 2 上调用
iframe
(site2:page1
)。假设iframe
ID 属性为"myframe"
。site2:page1:运行JavaScript(在
body.onload
中)来计算当前视图的大小(我认为你应该使用document.scrollTop< /代码> 等)。同时运行
iframe
(site1:page2
),在查询字符串中传递所需的宽度。site1:page2:读取查询字符串并找出请求的高度,更新
parent.parent.document.getElementById("myframe").style.height
< /p>I am sorry if you find this reply to be incomplete (it is). I just don't have code samples with me. I'll sketch the main concept and you will have to fill out the details yourself.
You have to use some tricks to do that since XSS security does not allow frames from one site to access frames from another site:
site1:page1: call
iframe
(site2:page1
) on site two. Let's assume theiframe
ID property is"myframe"
.site2:page1: run JavaScript (in
body.onload
) to calculate the size of the current view (I think you should usedocument.scrollTop
etc.). Also run aniframe
(site1:page2
) passing the needed width in the query string.site1:page2: read the query string and find out the requested height, update
parent.parent.document.getElementById("myframe").style.height
如果只是另一个子域,
document.domain
< /a> 应该为您提供帧之间所需的访问权限。If it's just another subdomain,
document.domain
should give you the needed access between frames.