P粉0927785852023-08-25 08:07:08
In Internet Explorer 8, events passed as parameters may be null, which is why you need to access the events differently:
In frame.html:
window.onmessage = function(event) { var evt = event || window.event; evt.source.postMessage('Message from iFrame', evt.origin); };
On main.html:
window.onmessage = function(event) { var evt = event || window.event; alert(evt.data); };
The event is triggered the same way as presented by Rob W:
document.getElementById('frameId').contentWindow.postMessage('message','*');
P粉6708387352023-08-25 00:50:36
If you have no control over the website being framed, there is no way to circumvent the cross-origin policy.
If you have control over both sites, you can use the postMessage
method of transferring data across domains. A very basic example:
// framed.htm: window.onmessage = function(event) { event.source.postMessage(document.body.innerHTML, event.origin); }; // Main page: window.onmessage = function(event) { alert(event.data); }; // Trigger: // <iframe id="myframe" src="framed.htm"></iframe> document.getElementById('myframe').contentWindow.postMessage('','*');