Why is html5 postMessage not working for me?

后端 未结 1 1832
情话喂你
情话喂你 2021-02-10 11:02

I use a few lines of javascript to create an iframe element, and then I\'d like to send it a message, like this:

function loadiframe (callback) {
    var body =          


        
相关标签:
1条回答
  • 2021-02-10 11:31

    There are two possible problems:

    1. You're calling callback before the child frame has loaded - try it in the load event or do it in a setTimeout
    2. I've never managed to get postMessage to work with anything other that '*' as the origin. If I put a URL in there I get a security warning "Security Error: Content at http://xxx/ may not load data from http://yyy/", except only in the error console (Ctrl + Shift + J) not in any other place. I suspect there's some other stuff that needs set up somewhere for that to work, but I've not yet figured out what.

    Here's a jsfiddle with a slightly modified version of your code loading a document off my domain, works for me in Firefox and Chrome.

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