码迷,mamicode.com
首页 > Windows程序 > 详细

跨域(六)——window.name

时间:2014-12-20 20:51:34      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

window.name也可以进行跨域数据传输。

下面是相应的代码,evil.html跨域读取foo.html的数据,其中proxy.html和evil.html同域,没有任何内容。

evil.html:

<script type="text/javascript">
    var flag= 0;
    var iframe = document.createElement(iframe);
    iframe.src = http://www.foo.com:8081/langtao/foo.html;
    document.body.appendChild(iframe);
    if (iframe.addEventListener) {
        iframe.addEventListener(load, hadle,false);
    } else {
        iframe.attachEvent(onload,hadle);
    }
    function hadle() {
        if (flag===1) {
            var data = iframe.contentWindow.name;    // 读数据
            alert(data);    //显示跨域数据
            //清理工作
            iframe.contentWindow.document.write(‘‘);
            iframe.contentWindow.close();
            document.body.removeChild(iframe);
        } else if (flag === 0) {
            flag = 1;
            iframe.contentWindow.location = "http://www.evil.com:8081/langtao/proxy.html";    // 设置的代理文件,proxy.html与evil.html同域
        }  
    };
</script>

foo.html:

<script type="text/javascript">
    window.name=我来自foo;    // name 属性可设置或返回存放窗口的名称的一个字符串,这里是我们要传输的数据
</script>

 

跨域(六)——window.name

标签:

原文地址:http://www.cnblogs.com/aaron-shu/p/4175793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!