跨域 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...
有什么想法吗?
推荐答案
Page inside 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屋!