龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > Javascript编程 >

js跨域问题浅析及解决方法优缺点对比(3)

时间:2014-11-09 03:01来源:网络整理 作者:网络 点击:
分享到:
复制代码 代码如下: iframe id = "iframe" src=" http://example.com/b.html " onload = "test()"/iframe script type="text/javascript" document.domain = 'example.com';//设置成主域 function test

复制代码 代码如下:

<iframe id = "iframe" src="http://example.com/b.html" onload = "test()"></iframe>
<script type="text/javascript">
    document.domain = 'example.com';//设置成主域
    function test(){
        alert(document.getElementById('iframe').contentWindow);//contentWindow 可取得子窗口的 window 对象
    }
</script>

2.在页面 http://example.com/b.html 中也设置document.domain:

复制代码 代码如下:

<script type="text/javascript">
    document.domain = 'example.com';//在iframe载入这个页面也设置document.domain,使之与主页面的document.domain相同
</script>

修改document.domain的方法只适用于不同子域的框架间的交互。

精彩图集

赞助商链接