Puppollo
4/24/2014 - 8:38 AM

cross-domain messaging

cross-domain messaging

//In the iframe (which has an element with an id of "foo" enclosing the main content):

function postSize(e){
  var target = parent.postMessage ? parent : (parent.document.postMessage ? parent.document : undefined);
  if (typeof target != "undefined" && document.body.scrollHeight)
    target.postMessage(document.getElementById("foo").scrollHeight, "*");
}
window.addEventListener("load", postSize, false);
//------------------------------------------------------------------------------------------------------------------------------------------------------------

//In the parent document (which contains the iframe with an id of "bar"):

function receiveSize(e){
  if (e.origin === "http://www.example.com") // for security: set this to the domain of the iframe - use e.uri if you need more specificity
    document.getElementById("bar").style.height = e.data + "px";
}
window.addEventListener("message", receiveSize, false);