Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript addEventListener在firefox和chrome中不起作用_Javascript_Google Chrome_Firefox - Fatal编程技术网

Javascript addEventListener在firefox和chrome中不起作用

Javascript addEventListener在firefox和chrome中不起作用,javascript,google-chrome,firefox,Javascript,Google Chrome,Firefox,我使用postMessage函数向页面中的iframe发送消息,如下所示 $(document).ready(function(){ document.getElementById('test').contentWindow.postMessage(message, '*'); )};//test is the id of my iframe in a.html 在b.html中,我使用window.addEventListener('message',onmessage,false)

我使用postMessage函数向页面中的iframe发送消息,如下所示

$(document).ready(function(){
    document.getElementById('test').contentWindow.postMessage(message, '*');
)};//test is the id of my iframe in a.html
在b.html中,我使用
window.addEventListener('message',onmessage,false)接收此消息:

window.onload = function() { 
    var onmessage = function(e) { 
        document.getElementById('display').innerHTML = e.data; 
    }; 
    window.addEventListener('message', onmessage, false); 
}; //this is the javascript in b.html

它在
IE10
中运行良好,但在
chrome
firefox
中我无法收到任何消息!为什么?

如果浏览器是最新的,请检查您的Chrome和FireFox更新

试试

 alert(onmessage);

如果他们有任何消息输出。

如果浏览器是最新的,请检查您的Chrome和FireFox更新

试试

 alert(onmessage);

如果他们有任何消息输出。

不要将代码放在window.onload中,可能在调用postMessage时,iframe onload尚未触发

 var onmessage = function(e) { 
        console.log(e);
    };
if ( typeof window.addEventListener != 'undefined') {
            window.addEventListener('message', onmessage, false);
          } else if ( typeof window.attachEvent != 'undefined') {
            window.attachEvent('onmessage', onmessage);
          }

不要将代码放在window.onload中,可能在调用postMessage时,如果未触发rame onload

 var onmessage = function(e) { 
        console.log(e);
    };
if ( typeof window.addEventListener != 'undefined') {
            window.addEventListener('message', onmessage, false);
          } else if ( typeof window.attachEvent != 'undefined') {
            window.attachEvent('onmessage', onmessage);
          }

$(document).ready
将在
DOMContentLoaded
上运行,这可能发生在子帧加载完成之前。因此,在将侦听器添加到子帧之前,您很有可能正在父帧中发送消息。

$(文档)。ready
将在
DOMContentLoaded
上运行,这可能发生在加载子帧之前。因此,在将侦听器添加到子对象之前,很有可能在父对象中发送消息。

我尝试过,但是。。。它仍然无法接收任何内容。在
IE10
类型的窗口中。addEventListener
始终是一个
函数
。IE10和IE11支持W3C事件模型,所以它们有addEventListener方法。您可以将html文件放在http服务器中,或者直接由broswer打开?由broswer打开即可。我尝试了,但。。。它仍然无法接收任何内容。在
IE10
类型的窗口中。addEventListener
始终是一个
函数
。IE10和IE11支持W3C事件模型,因此它们有addEventListener方法。您可以将html文件放在http服务器中,或者直接由broswer打开?只需由broswer打开即可。是的,我的浏览器是最新的。
警报(onmessage)
onmessage
只是一个函数。if(window.XMLHttpRequest){//IE7+、Firefox、Chrome、Opera、Safari的代码xmlhttp=new XMLHttpRequest();}否则{//IE6、IE5的代码xmlhttp=new ActiveXObject(“Microsoft.xmlhttp”);}复制上面的代码。这是检查浏览器是否适合您的代码是我的浏览器是最新的。并且
alert(onmessage)
onmessage
只是一个函数。if(window.XMLHttpRequest){//code for IE7+,Firefox,Chrome,Opera,Safari xmlhttp=new XMLHttpRequest();}其他{//IE6的代码,IE5 xmlhttp=new ActiveXObject(“Microsoft.xmlhttp”);}复制上面的代码。这是检查浏览器是否适合您的代码。那么,我应该怎么做才能在
chrome
firefox
中正确发送消息?它可以在IE10中工作,您能告诉我为什么吗?这是原因。我不知道
加载()
之前的函数。那么,我应该怎么做才能在
chrome
firefox
中正确发送消息?它可以在IE10中工作,你能告诉我为什么吗?这是原因。我以前不知道
load()
函数。