IFrame to IFrame communication
Is there any way for IFrame
to IFra开发者_StackOverflow中文版me
communication through JavaScript
If the iFrames share the same domain origin, you can directly call Javascript or read/write variables into each iFrame from the other. You get the iFrame object and get the web-page's window object from that.
If the iFrames do not have the same origin, then modern browsers will prevent you from communicating with normal javascript. The only way to pass info is with messaging which is a relatively new browser capability and requires pre-cooperation among the code of both iFrames.
As jfriend00 said, you can't call javascript across domain. This would violate the Same Origin Policy.
One of the new features of HTML5 is postMessage. This allows you to send a message to an iframe. It then requires a message listener on the content of the iframe to process your message.
Just go via the parent
and frames.frameId
with contentWindow
and contentDocument
properties, keeping in mind the same origin policy
As stated in the other answers there are a few different ways to communicate between the iframe and parent doc, but require control over the iframe document. (postmessage, etc) However, it seems like this will soon become a lot easier as webkit has already implemented new attributes for the iframe - one controlling this behavior and specifically allowing for removing iframe restrictions - namely same domain. While interesting its only html5/webkit but shows some promise for cool future possibilities with iframes http://www.w3schools.com/html5/att_iframe_sandbox.asp
One of the way we can communicate between iframes by using dojo library dojox cometd dojox.cometd.
Using this we can push every javascript function to another iframe or other page.
We can communicate between different computer with different browser or iframe using same domain but its limitation is we cannot communicate using different domain.
If what you need is to pass a value from one iFrame to other, you can achieve that by setting a hidden field in the parent iFrame, and then access it from the child:
It would be something like this:
in the parent:
var input = document.createElement("input" );
input.setAttribute( "type", "hidden" );
input.setAttribute( "id", "combinationType" );
input.setAttribute( "value", "very cool!!!" );
document.getElementsByTagName( 'div')[0].appendChild(input);
In the Child:
var val = window.parent.document.getElementById('combinationType').value;
I hope it helps.
精彩评论