Skip to content

Instantly share code, notes, and snippets.

@jimmyjacobson
Last active August 29, 2015 14:08
Show Gist options
  • Save jimmyjacobson/e4b42ef5cbad789e268c to your computer and use it in GitHub Desktop.
Save jimmyjacobson/e4b42ef5cbad789e268c to your computer and use it in GitHub Desktop.
Cross Domain Message Passing
/*
This code goes in the webpage that is embedded via iframe.
Use of JQuery is assumed in this example
*/
function postMessage() {
var msg = $('body').height();
window.parent.postMessage(msg, '*');
}
//Event handler for window resize
$(window).resize(postMessage);
//Event handler for page load
$().ready(postMessage);
/*
This code is loaded by the parent webpage. It listens for a message
and sets the height of the element with id = id-of-iframe to the value in the message.
This is pretty simple code. In a production environment there are many things posting messages
so you should be careful about filtering for the specific message from your app.
*/
function msgReceiver(ev){
var msg = ev.data;
var elem = document.getElementById('id-of-iframe');
if (elem) {
elem.style.height = (parseInt(msg)) + "px";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment