Javascript communicating cross-domain to parent window of iframe

前端 未结 1 1388
余生分开走
余生分开走 2020-12-16 08:29

I have written a tool for taking notes, grabbing images from web pages. It loads itself as a iFrame within the current window by using a javascript bookmark:



        
相关标签:
1条回答
  • 2020-12-16 09:13

    postMessage is probably what you're looking for. Mozilla has documented this and it has fairly decent cross browser support:

    https://developer.mozilla.org/en-US/docs/DOM/window.postMessage

    I also wrote a library around this concept, it may need a little debugging but it is available on github: https://github.com/tsharp/OF.Core.js/blob/master/js/of/window.messaging.js

    From here you'll need an event listener on the parent window to handle all incoming requests ... which will remove the iframe from the parent context. Here is an example of registering the message received event.

    function registerWindowHandler() {
        if (typeof window.addEventListener !== 'undefined') {
        window.addEventListener('message', receiveMessage, false);
        } else {
        // Support for ie8
        window.attachEvent('onmessage', receiveMessage);
        }
    }
    
    0 讨论(0)
提交回复
热议问题