Cross domain iframe issue

前端 未结 5 1717
[愿得一人]
[愿得一人] 2020-11-21 06:14

For say i have a Site called example.com on which iframe is embedded of domain iframe.net, now i want to read the content of iframe and pass some parameter to display a text

5条回答
  •  无人共我
    2020-11-21 07:05

    In Internet Explorer 8, events passed as a parameter may be null, that is why you need to access the event in a different manner:

    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 Rob W has presented:

    document.getElementById('frameId').contentWindow.postMessage('message','*');
    

提交回复
热议问题