开发者

Cross-domain iframe communication in Opera

开发者 https://www.devze.com 2022-12-19 19:13 出处:网络
I have need to communicate between two iframes of the same domain, which live inside a parent page on a different domain that I have no control over.

I have need to communicate between two iframes of the same domain, which live inside a parent page on a different domain that I have no control over.

This is a Facebook app and the basic layout is this

apps.facebook.com/myapp

L iframe1 (src='mysite.com/foo')

L iframe2 (src='mysite.com/bar')

I need frame1 to talk to frame2, b开发者_运维百科ut in Opera I can't access window.parent.frames['frame2'] to do the usual cross-domain methods (updating location.hash for example)

Is there an alternate way to accomplish this in Opera?

Thanks for your help in advance


Did you try using HTML5 web messaging. It is quite well supported currently by recent versions of browsers.

iframe.contentWindow.postMessage('Your message','http://mysite.com');

The postMessage property will need the origin http://mysite.com.


Generally no. Same Origin Policy denies you the possibility of communicating upwards to the parent, which would be necessary to then step downwards to the other frame. This is true in any browser.

If the parent document has given your frame-to-be-contacted a unique name, there is a limited form of communication possible with it by getting the user to click a link with href="otherurl#message" target="name", which will navigate the target frame by changing the hash without reloading the page, as long as the URL matches exactly. In Mozilla you can also do this with a form target, allowing you to script its submission (since link clicking cannot be automated), but not in Opera. Probably not much use... don't know if FB gives you a frame target name in any case.

You can make a communication channel between scripts in the same domain by using cookies(*): one script writes a session cookie, the other script polls for changes to document.cookie to find messages in it. But it's super-ugly and requires some annoying work to control signalling which messages are meant for whom when there are multiple documents open simultaneously. And there are further limitations for cookies in third-party frames (you will probably need to write a P3P policy to get IE to co-operate).

(*: or, presumably, HTML5 web storage, where available.)


As others have said, use window.postMessage. But instead of using window.parent.frames['frame2'], try window.parent.frames[x] where x is the position in the node list of the other iframe.

You can see an example of doing this across origins here: http://webinista.s3.amazonaws.com/postmessage

0

精彩评论

暂无评论...
验证码 换一张
取 消