Iframe Cross Domain Issue
I want to pass a message from iframe to parent page in every few seconds like: Iframe Domain = www.abc.com Parent Domain = www.xyz.com Have check with: Cross domain iframe issue
Solution 1:
I just recently helped another person with a very similar concern, passing messages between IFrames. (see Issues with Cross Document Messaging between IFrame & Parent).
Using a modified version of the code that I am borrowing from suamikim in that aforementioned topic, I have integrated a timer. This should serve as a good starting point for you. These parent and child html pages will work cross-domain just as described in the comments above by Amadan. I just tested and confirmed it, by placing parent.html on one domain, which pointed to child.html on a totally separate untrusted domain.
parent.html
<html><head><scripttype="text/javascript">functionparentInitialize() {
var count = 0;
window.addEventListener('message', function (e) {
// Check message origin etc...if (e.data.type === 'iFrameRequest') {
var obj = {
type: 'parentResponse',
responseData: 'Response #' + count++
};
e.source.postMessage(obj, '*');
}
// ...
})
}
</script></head><bodystyle="background-color: rgb(72, 222, 218);"onload="javascript: parentInitialize();"><iframesrc="child.html"style="width: 500px; height:350px;"></iframe></body></html>
child.html
<html><head><scripttype="text/javascript">functionchildInitialize() {
// ...try {
if (window.self === window.top) {
// We're not inside an IFrame, don't do anything...return;
}
} catch (e) {
// Browsers can block access to window.top due to same origin policy.// See https://stackoverflow.com/a/326076// If this happens, we are inside an IFrame...
}
functionmessageHandler(e) {
if (e.data && (e.data.type === 'parentResponse')) {
// Do some stuff with the sent datavar obj = document.getElementById("status");
obj.value = e.data.responseData;
}
}
window.addEventListener('message', messageHandler, false);
setInterval(function () {
window.parent.postMessage({ type: 'iFrameRequest' }, '*');
}, 1000);
// ...
}
</script></head><bodystyle="background-color: rgb(0, 148, 255);"onload="javascript: childInitialize();"><textareatype="text"style="width:400px; height:250px;"id="status" /></body></html>
Good luck!
Post a Comment for "Iframe Cross Domain Issue"