using postmessage to refresh iframe's parent document

前端 未结 1 1199
面向向阳花
面向向阳花 2020-12-02 13:35

I have a greasemonkey script that opens an iframe containing a form from a different sub-domain as the parent page.

I would like to refresh the parent page when t

相关标签:
1条回答
  • 2020-12-02 14:23

    Use:

    window.parent.postMessage('Hello Parent Frame!', '*');
    

    Note the '*' indicates "any origin". You should replace this with the target origin if possible.

    In your parent frame you need:

    window.addEventListener('message', receiveMessage, false);
    
    function receiveMessage(evt)
    {
      if (evt.origin === 'http://my.iframe.org')
      {
        alert("got message: "+evt.data);
      }
    }
    

    Replace "my.iframe.org" with the origin of your iFrame. (You can skip the origin verification, just be very careful what you do with the data you get).

    0 讨论(0)
提交回复
热议问题