Talking to other frames in Javascript

Want to talk between iframes?
Tested in Safari 8.0.2 (10600.2.5), Firefox 35.0 and Chrome 40.0.2214.91 (64-bit) on MacOS

Communication between a parent and a child frame

Parent: “Hi there kiddo, was your content by any chance changed?”
Child : ‘Yes it was father!’
Parent: “Okay, let’s not close this tab then, but instead show your regular change reminder!”


Parent: “Hi there kiddo, was your content by any chance changed?”
Child : ‘No it was not.’
Parent: “Okay, let’s close this tab then!”

Actual code


* Remove a tab and tab-content
function closeTab(tabId){
	var test = document.getElementById('content_'+tabId).contentWindow.hasWeekscheduleChangedIframe();
	// If the test was true, something changed in child, escape from function
	// otherwise just continue and close the tab
	if(true === test){

actually close tab.


// Communication with parent page
function hasWeekscheduleChangedIframe(){
		// View popup with options to discard changes, save changes or continue without action
		$('#changedWeekschedulePopup').modal({backdrop: 'static'});
		window.parent.parent.scrollTo(0, scrollTopLocation);
		return true;
	return false;


In other words, to call a function within a child iframe, use the following code:


Of course assuming that elementId is the id of an iframe.

Courtesy of this StackOverflow thread.

One thought on “Talking to other frames in Javascript

Leave a Reply

Your email address will not be published. Required fields are marked *