Same domain:
1. child iframe calls parent window JS function:
window.parent.functionName()
2. parent window calls iframe JS function:
document.getElementById('frameName').contentWindow.functionName()
Other alternatives for above JS code
window.frames['frameName'].contentWindow.functionName();
var el = document.getElementById('frameName');
if(el.contentWindow) {
el.contentWindow.functionName();
}
else if(el.contentDocument) {
//el.contentDocument.functionName();
el.contentDocument.defaultView.functionName();
}
Cross domain:
HTML5 Web Messaging http://dev.w3.org/html5/postmsg/#web-messaging defines cross document messaging to address this issue, and as of writing, it is supported in most browsers (except for IE7 and below) for iFrame-to-parent-window communication (IE has not implemented cross window/tab messaging yet). The standard API is window.postMessage(message, targetOrigin [,transfer]).
However if we need support old browsers for cross domain messaging, we need look at other hacks or solutions.
- Hashchange of iframe or parent window. For instance, parent window change iframe window's fragment (hashchange), and iframe polling the change (or onhashchange event) to get passed fragment identifier
- hidden proxy iframe, hashchange based
- hidden proxy iframe, resize event based (see https://github.com/ternarylabs/porthole/)
- Flash (some library uses flash fallback)
Client to server cross domain:
- proxy server
- JSONP
- CORS (Cross origin resource sharing)
http://softwareas.com/cross-domain-communication-with-iframes
http://stackoverflow.com/questions/6309674/wrapping-postmessage-in-jquery
No comments:
Post a Comment