跨域iframe通信 [英] Cross-Domain iframe communication
问题描述
我在网页上创建了iframe,并且网页的网域已明确设置为xyz.com,但iframe的网域默认为dev.xyz.com,这是我真正的网域开发。
I have an iframe being created on a page, and the page's domain is being explicitly set to 'xyz.com' but the iframe's domain is defaulting to 'dev.xyz.com', which is the actual domain i'm developing for.
问题是,当我尝试通过iframe.contentWindow.document访问该iframe时,由于域的不同而失败。
The problem is, when I try to access that iframe via iframe.contentWindow.document, it fails due to the difference in domain.
我已经尝试设置iframe的src到一个文件与document.domain ='xyz.com',但似乎没有做的技巧...
I've tried setting the iframe's src to a file with document.domain = 'xyz.com' but that doesn't seem to be doing the trick...
任何想法?
推荐答案
iframe中的页面:
Page inside iframe:
<script>
document.domain = document.domain;
</script>
看起来很傻,但它工作。请参见 document.domain = document.domain是什么? 。
It looks silly, but it works. See "What does document.domain = document.domain do?".
这篇关于跨域iframe通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!