简体   繁体   English

如何从iframe中获取当前iframe偏移量?

[英]How do I get the current iframe offset from within the iframe?

I expect security restrictions (cross-site scripting) will prevent me from doing this, but I thought asking wouldn't hurt. 我希望安全限制(跨站点脚本)会阻止我这样做,但我认为要求不会受到伤害。 I'm working on a page that may be embedded within another page from a different domain. 我正在处理可能嵌入来自不同域的另一个页面的页面。 I don't know anything about the iframe, except I could predict part of the src attribute. 我对iframe一无所知,除了我可以预测部分src属性。 I can detect "if" it's embedded, but I really need to know the offset at which it's embedded. 我可以检测到它是否嵌入了“if”,但我真的需要知道它嵌入的偏移量。

I'm doing some position calculations within my page (that in this case is embedded in an iframe). 我正在我的页面中进行一些位置计算(在这种情况下嵌入在iframe中)。 It works great whether or not it's embedded in all current browsers but gives me problems in IE 7. For some reason the offset jQuery gives me in IE 7 is off by the exact amount of the embedded iframe offset. 无论它是否嵌入到所有当前浏览器中,它都很有效,但在IE 7中给我带来了问题。由于某种原因,偏移量jQuery让我在IE 7中偏离了嵌入式iframe偏移的确切数量。 I could compensate for it in this case, if I could get the offset. 如果我能得到偏移,我可以在这种情况下补偿它。

Thanks in advance. 提前致谢。

You can do this with JavaScript. 你可以用JavaScript做到这一点。

Parent Frame 父框架

<html>
<body>
    <iframe src="childFrame.html"></iframe>
</body>
</html>

Child Frame 儿童框架

<html>
<body>
   Child Frame
</body>
<script type="text/javascript">
    alert(document.parentWindow.document.body.innerHTML);
</script>
</html>

The key bit is here "document.parentWindow.document.body". 关键位在这里是“document.parentWindow.document.body”。 Now you have the parent body you can access the HTML and get any properties you need. 现在您拥有了父体,您可以访问HTML并获取所需的任何属性。

The other way round you can use the Sandbox attribute on the Iframe. 反过来,您可以使用Iframe上的Sandbox属性。 Check out the link below. 看看下面的链接。 This attribute is not recommended for production code. 不建议将此属性用于生产代码。

http://www.w3schools.com/tags/tag_iframe.asp http://www.w3schools.com/tags/tag_iframe.asp

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

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