Skip to content Skip to sidebar Skip to footer

Javascript --> Iframe Resizing Using Postmessage Method

I'm trying to understand another Stackoverflow answer (cross-domain iframe resizer?) that purports to solve how to resize an iframe (hosted on a domain separate from the domain it'

Solution 1:

I needed to do something similar, and found this example that seems simpler: using postmessage to refresh iframe's parent document

Here is what I ended up with in the iframe:

window.onload = function() {
  window.parent.postMessage(document.body.scrollHeight, 'http://targetdomain.com');
}

And in the receiving parent:

window.addEventListener('message', receiveMessage, false);

functionreceiveMessage(evt){
  if (evt.origin === 'http://sendingdomain.com') {
    console.log("got message: "+evt.data);
    //Set the height on your iframe here
  }
}

Post a Comment for "Javascript --> Iframe Resizing Using Postmessage Method"