简体   繁体   English

如何通过DOM / Javascript访问外部iframe的内容?

[英]How can you access an external iframe's contents via the DOM/Javascript?

I have a page thusly: 我这样有一个页面:

<html>
    <head></head>
    <body>
        <iframe src="local.html"></iframe>
        <iframe src="http://www.google.com"></iframe>
    </body>
</html>

I've used the DOM to access the first iframe as a test (node.documentWindow) but when I try similar on the external iframe Firebug reports that access is denied. 我已经使用DOM来访问第一个iframe作为测试(node.documentWindow),但是当我在外部iframe上尝试类似的时,Firebug报告访问被拒绝。

I suspect this is for XSS protection, but is there a "safe" way to import the node so I can grab an element from that external page? 我怀疑这是用于XSS保护,但有一种“安全”方式导入节点,所以我可以从该外部页面获取一个元素? Is there a way to explore the "document as rendered" or something? 有没有办法探索“渲染文档”或什么?

Thanks! 谢谢!

Nope. 不。 Cross domain security prevents this. 跨域安全性阻止了这一点。 The only way around is if the surrounding page, and the iframe, are on different subdomains on the same domain. 唯一的方法是,如果周围的网页和iframe位于同一个域中的不同子域中。 In that case, you can use document.domain . 在这种情况下,您可以使用document.domain

This is pretty much a given. 这几乎是一个给定的。 Imagine the security implications if this were not the case. 如果不是这种情况,想象一下安全隐患。 You could build an iframe containing a user's home banking page, and grab their password using keydown , for example. 您可以构建一个包含用户主页银行页面的iframe,并使用keydown获取其密码。 There's tons of possibilities of misuse. 滥用的可能性很大。

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

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