美文网首页
跨域-document.domain

跨域-document.domain

作者: duJing | 来源:发表于2017-01-04 14:48 被阅读204次

    上一篇七种跨域方法【1.CROS篇】主要解决的是异域之间的传值 这里主要解决的是子域与父域之间的传值 问题描述: 现有父域:http://b.com/b.com.html 要向子域:http://a.b.com/a.b.com.html获取数据 怎么办? 将document.domain = 'b.com';都设置为父域即可

    如果不知道如何配置虚拟主机?
    http://blog.csdn.net/super_yang_android/article/details/53991982
    父域:http://b.com/b.com.html内容
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
    <script>
        document.domain = 'b.com';
        var ifr = document.createElement('iframe');
        ifr.src = 'http://a.b.com/a.b.com.html';
        ifr.style.display = 'none';
        document.body.appendChild(ifr);
        ifr.onload = function(){
            var doc = ifr.contentDocument || ifr.contentWindow.document;
            // 这里操作DOM
            var oUl = doc.getElementById('ul1');
            alert(oUl.innerHTML);
            ifr.onload = null;
        };
    </script>
    </body>
    </html>
    子域:http://a.b.com/a.b.com.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <script>
        document.domain = 'b.com';
    </script>
    <ul id="ul1">我是子域a.b.com中的UL</ul>
    </body>
    </html>

    相关文章

      网友评论

          本文标题:跨域-document.domain

          本文链接:https://www.haomeiwen.com/subject/cniovttx.html