问题:Javascript iframe在iframe完成加载之前发送postMessage()

问题:Javascript iframe在iframe完成加载之前发送postMessage(),javascript,iframe,Javascript,Iframe,我在parent.html中有iframe。Child.html将postMessage('documnent.cookie','*')发送到父窗口 问题是postMessage()发送“null”。在iframe加载完成之前触发postMessage()。只有在iframe完全加载数据时,我才必须将postMessage()发送到父窗口 这是我的代码:parent.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999

我在parent.html中有iframe。Child.html将postMessage('documnent.cookie','*')发送到父窗口

问题是postMessage()发送“null”。在iframe加载完成之前触发postMessage()。只有在iframe完全加载数据时,我才必须将postMessage()发送到父窗口

这是我的代码:parent.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

<body id="Body" style="background-color:Silver">

  <iframe id ="CA_FRAME_0" src="http://localhost/ibmcognos/bi/child.html" style="display:none"></iframe>

  <script type="text/javascript">  

    window.addEventListener("message", function (e){   


    if(e.data == null){         
        alert('fail');                

    }else{      
        alert('sucess');           
        document.cookie="key=" +e.data + ";expires=0;path=/";           
    }       

    }, false);  


</script>

<div id="arcContainer" class="arcContainer"></div>  

    <script type="text/javascript">     

        ARCPLAN.language = "XXX";
        ARCPLAN.application = "lv02";
        ARCPLAN.startDocName = "application.apa";
        ARCPLAN.arcCgiSite = "http://localhost/.....";      

    </script>
</body>
</html>

addEventListener(“消息”,函数(e){
如果(e.data==null){
警报(“失败”);
}否则{
警惕(“成功”);
document.cookie=“key=“+e.data+”;expires=0;path=/”;
}       
},假);
ARCPLAN.language=“XXX”;
ARCPLAN.application=“lv02”;
ARCPLAN.startDocName=“application.apa”;
ARCPLAN.arcCgiSite=”http://localhost/.....";      
//child.html

 <!DOCTYPE html> <html> <head>
    <title>COOKIE</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" /> </head> <body>

<script type="text/javascript">

    function getCookie(name) {      var arg = name + "=";       var alen = arg.length;      var clen = document.cookie.length;      var i = 0;      while(i < clen) {           var j = i
+ alen;             if(document.cookie.substring(i, j) == arg)
                return getCookieVal(j);             i = document.cookie.indexOf(" ", i) + 1;            if(i == 0)
                break;      }       return null;    }

    function getCookieVal(offset) {         var endstr = document.cookie.indexOf(";", offset);      if(endstr == -1)            endstr = document.cookie.length;        return document.cookie.substring(offset, endstr);   }   
            **parent.postMessage(getCookie("key"), "*");**   </script>

**<iframe id ="CA_FRAME" src="http://localhost/ibmcognos/bi/" style="display: none;" ></iframe>**   - *this url make the redirect from here and set the cookie, it takes time* 

</body> </html>
<html> 
    <body>
        CHILD - I send data to parent - but only after CA_FRAME has loaded
        <br /><br />
        <iframe id ="CA_FRAME" src="inner.html" onload="onCaFrameLoad(this)"></iframe>
        <script>
            function onCaFrameLoad() {
                alert('iframe has loaded! now you can send data to the parent');
            };
        </script>
    </body>
</html>

曲奇
函数getCookie(name){var arg=name+“=”;var alen=arg.length;var clen=document.cookie.length;var i=0;而(i

请给我一些建议。谢谢。

在iFrame中添加一个onload事件:

<iframe id ="CA_FRAME" src="http://localhost/ibmcognos/bi/" onload="onCaFrameLoad(this)" style="display: none;"></iframe>
这样,代码只会在框架完成加载后运行

PoC: parent.html

<html>
    <body>
        PARENT- I get data from child
        <br /><br />
        <iframe id ="CA_FRAME_0" src="child.html"></iframe>
    </body>
</html>

父级-我从子级获取数据


child.html

 <!DOCTYPE html> <html> <head>
    <title>COOKIE</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" /> </head> <body>

<script type="text/javascript">

    function getCookie(name) {      var arg = name + "=";       var alen = arg.length;      var clen = document.cookie.length;      var i = 0;      while(i < clen) {           var j = i
+ alen;             if(document.cookie.substring(i, j) == arg)
                return getCookieVal(j);             i = document.cookie.indexOf(" ", i) + 1;            if(i == 0)
                break;      }       return null;    }

    function getCookieVal(offset) {         var endstr = document.cookie.indexOf(";", offset);      if(endstr == -1)            endstr = document.cookie.length;        return document.cookie.substring(offset, endstr);   }   
            **parent.postMessage(getCookie("key"), "*");**   </script>

**<iframe id ="CA_FRAME" src="http://localhost/ibmcognos/bi/" style="display: none;" ></iframe>**   - *this url make the redirect from here and set the cookie, it takes time* 

</body> </html>
<html> 
    <body>
        CHILD - I send data to parent - but only after CA_FRAME has loaded
        <br /><br />
        <iframe id ="CA_FRAME" src="inner.html" onload="onCaFrameLoad(this)"></iframe>
        <script>
            function onCaFrameLoad() {
                alert('iframe has loaded! now you can send data to the parent');
            };
        </script>
    </body>
</html>

子级-我向父级发送数据-但仅在加载CA_帧之后


函数onCaFrameLoad(){ 警报('iframe已加载!现在您可以将数据发送到父级'); };
inner.html

<html> 
    <body>
        INNER - I load all my content, then the onCaFrameLoad function in child.html runs
        <script>
        alert('My Name is INNER - I load all my content first');
        </script>
    </body>
</html>

内部-我加载所有内容,然后child.html中的onCaFrameLoad函数运行
警报(“我的名字是内部的-我首先加载所有内容”);

谢谢您的回复。我以前试过,现在也试过,但面临同样的问题。我在父母和孩子身上各有一个iframe。它来自不同的来源。哪个iframe发送给另一个?父到子还是子到父?child.html使用postMessage('key','*')将cookie数据发送到Parent.html“key”是来自子iframe url的一个cookie数据。您是否尝试将onload事件添加到父iframe,并将侦听器js添加到子脚本中?我还不明白。。我怎样才能做到。我只需要从孩子到家长的数据,而不是从孩子到家长的数据。如果可能,请澄清我或显示一些示例片段。