iframe跨域访问

js跨域是个讨论很多的话题。iframe跨域访问也被研究的很透了。

一般分两种情况:

一、 是同主域下面,不同子域之间的跨域;

  同主域,不同子域跨域,设置相同的document.domian就可以解决;

     父页访问子页,可以document.getElementById("myframe").contentWindow.document来访问iframe页面的内容;如果支持contentDocument也可以直接document.getElementById("myframe").contentDocument访问子页面内容;

  子页访问父页,可以parent.js全局属性

二、 是不同主域跨域;

  前提,www.a.com下a.html,a.html内iframe调用了www.b.com下的b.html,b.html下iframe调用了www.a.com下的c.html

  b.html是不无法直接访问a.html的对象,因为涉及到跨域,但可以访问parent,同样c.html的parent可以访问b.html。c.html和a.html同域,是可以访问a下的对象的。parent.parent.js对象!

实例:

a.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <meta charset="utf-8" />
</head>
<body>
    <iframe src="http://localhost:19281/b.html"></iframe>
    <ul id="getText"></ul>
    <script>
    function dosome(text){
        document.getElementById("getText").innerHTML= decodeURI(text);
    }
    </script>
</body>
</html>

c.html

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <meta charset="utf-8" />
    <script>
        window.onload = function () {
            var text = window.location.href.split('=')[1]
            console.log(parent.parent)
            parent.parent.dosome(text);
        }
    </script>
</head>
<body>
    132132331
</body>
</html>

b.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <meta charset="utf-8" />
</head>
<body>
    <iframe id="myfarme" src="###"></iframe>
    <ul id="ct">
        <li>这里是内容1</li>
        <li>这里是内容2</li>
        <li>这里是内容3</li>
        <li>这里是内容4</li>
        <li>这里是内容5</li>
        <li>这里是内容6</li>
    </ul>
    <script>
    window.onload = function(){
        var text = document.getElementById('ct').innerHTML;
        document.getElementById('myfarme').src = "http://localhost:18731/a.html?content=" + encodeURI(text);
    }
    </script>
</body>
</html>

结果:

a.html

b.html

原文地址:https://www.cnblogs.com/bosamvs/p/6018268.html