How to communicate between iframe and the parent site?

JavascriptHtmlAjaxIframe

Javascript Problem Overview


The website in the iframe isn't located in the same domain, but both are mine, and I would like to communicate between the iframe and the parent site. Is it possible?

Javascript Solutions


Solution 1 - Javascript

With different domains, it is not possible to call methods or access the iframe's content document directly.

You have to use cross-document messaging.

parent -> iframe

For example in the top window:

myIframe.contentWindow.postMessage('hello', '*');

and in the iframe:

window.onmessage = function(e) {
	if (e.data == 'hello') {
		alert('It works!');
	}
};

iframe -> parent

For example in the top window:

window.onmessage = function(e) {
	if (e.data == 'hello') {
		alert('It works!');
	}
};

and in the iframe:

window.top.postMessage('hello', '*')

Solution 2 - Javascript

In 2018 and modern browsers you can send a custom event from iframe to parent window.

iframe:

var data = { foo: 'bar' }
var event = new CustomEvent('myCustomEvent', { detail: data })
window.parent.document.dispatchEvent(event)

parent:

window.document.addEventListener('myCustomEvent', handleEvent, false)
function handleEvent(e) {
  console.log(e.detail) // outputs: {foo: 'bar'}
}

PS: Of course, you can send events in opposite direction same way.

document.querySelector('#iframe_id').contentDocument.dispatchEvent(event)

Solution 3 - Javascript

This library supports HTML5 postMessage and legacy browsers with resize+hash https://github.com/ternarylabs/porthole

Edit: Now in 2014, IE6/7 usage is quite low, IE8 and above all support postMessage so I now suggest to just use that.

https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage

Solution 4 - Javascript

Use event.source.window.postMessage to send back to sender.

From Iframe

window.top.postMessage('I am Iframe', '*')
window.onmessage = (event) => {
    if (event.data === 'GOT_YOU_IFRAME') {
        console.log('Parent received successfully.')
    }
}

Then from parent say back.

window.onmessage = (event) => {
    event.source.window.postMessage('GOT_YOU_IFRAME', '*')
}

Solution 5 - Javascript

the window.top property should be able to give what you need.

E.g.

alert(top.location.href)

See http://cross-browser.com/talk/inter-frame_comm.html

Solution 6 - Javascript

You can also use

postMessage(message, '*');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanny FoxView Question on Stackoverflow
Solution 1 - Javascriptuser123444555621View Answer on Stackoverflow
Solution 2 - JavascriptStranger in the QView Answer on Stackoverflow
Solution 3 - JavascriptjpilloraView Answer on Stackoverflow
Solution 4 - JavascriptBinh HoView Answer on Stackoverflow
Solution 5 - JavascriptsambomartinView Answer on Stackoverflow
Solution 6 - JavascriptgonzarodrigueztView Answer on Stackoverflow