跨域JS传值时,postMessage 页面之间发送消息

postMessage方法

使用方法:

 

监控JS:

window.addEventListener( "message",

function(e){

},false);

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

top.postMessage("{msg:'你好'}", "www.hao123.com");//写top.postMessage或者是parent.message,对于top和parent区别,不要写window.postMessage

 

Failed to execute ‘postMessage’ on ‘DOMWindow’ 错误

出现Failed to execute ‘postMessage’ on ‘DOMWindow’,要用top.postMessage,不要用window.postMessage,其实top就是指向iframe最顶层的窗口,在我们这个例子中,因为主页面iframe,用window.postMessage会出现这个问题

你可能感兴趣的:(资料)