js跨域问题浅析及解决方法优缺点对比(3)
复制代码 代码如下: 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的方法只适用于不同子域的框架间的交互。
- 上一篇:jQuery函数map()和each()介绍及异同点分析
- 下一篇:js 操作符汇总
收藏文章
精彩图集
精彩文章