javascript - HTML5 - Cross Browser Iframe postmessage - parent to child communication - Stack Overflow

I wrote a content script that injects an iframe to any website (therefore different domain). I need the

I wrote a content script that injects an iframe to any website (therefore different domain).

I need the parent website to send some information to the child iframe, however I couldn't find a way to do it.

The code

var targetFrame = $('#myIframe')[0];
targetFrame.contentWindow.postMessage('the message', '*');

Doesn't work somehow and i get a Cannot call method 'postMessage' of undefined error. But then when I tried the same code directly in Chrome's console, it worked.

I had no trouble sending a postMessage from the child to the parent though but just need a way for the parent to send messages to the child iframe.

I wrote a content script that injects an iframe to any website (therefore different domain).

I need the parent website to send some information to the child iframe, however I couldn't find a way to do it.

The code

var targetFrame = $('#myIframe')[0];
targetFrame.contentWindow.postMessage('the message', '*');

Doesn't work somehow and i get a Cannot call method 'postMessage' of undefined error. But then when I tried the same code directly in Chrome's console, it worked.

I had no trouble sending a postMessage from the child to the parent though but just need a way for the parent to send messages to the child iframe.

Share Improve this question asked Aug 2, 2012 at 14:40 DracoDraco 2625 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

I recently wrote code that did postMessage to an iframe and I encountered quite a similar issue where it said contentWindow is undefined.

In my case, my iframe was not yet part of the DOM tree, it was a variable created by document.createElement('iframe').

Once I put it hidden (width and height 0px, visibility hidden) into the body of the page, contentWindow was no longer undefined and everything worked as expected.

I found the Mozilla Developer Network page for postMessage extremely useful when I was working on my project.

I've had success using the following library:

http://easyxdm/wp/

It doesn't require any flash/silverlight, only javascript. And it is patible as far back as as IE6.

It took a little doing to get it up and running, but once it was things ran very smoothly.

Keep in mind that if the iFrame you're opening on the other domain uses a different protocol (HTTP vs. HTTPS) the browser will kick out a warning which prevents your script from running (unless the user says they will accept the risk). If you have access to both protocols it may be wise to host the contents of the iFrame on both HTTP and HTTPS and load the appropriate script accordingly.

Good luck!

You don't need to target contentWindow. Try this:

var targetFrame = $('#myIframe')[0];
targetFrame.postMessage('the message', '*');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744377344a4571252.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信