Skip to content

Instantly share code, notes, and snippets.

@thetwosents
Last active August 29, 2015 14:17
Show Gist options
  • Save thetwosents/6b03a242819b172cbac1 to your computer and use it in GitHub Desktop.
Save thetwosents/6b03a242819b172cbac1 to your computer and use it in GitHub Desktop.
Cross-origin iframe height resize based on inner content of iframe
// This gist is intended for the age old problem of cross-origin iframe resizing for the height of an iframe to look like part of a site rather than a POS iframe.
// Put in the iframe code (after all of your content has loaded or at the end of success event for final ajax calls)
// This sends the height as an available variable for the parent page to grab.
window.parent.postMessage($(document).height(), "*");
// In the parent page code. Initialize with onload="resizeIframe()".
// This grabs that variable and then sets the iframe height based on whatever is being sent back from the iframe
function resizeIframe() {
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
eventer(messageEvent, function (e) {
$('{{IFRAMEID-GOES-HERE}}').css('height',e.data);
}, false);
}
// Credit to Tadit Dash ( http://www.codeproject.com/Tips/585663/Communication-with-Cross-Domain-IFrame-A-Cross-Bro )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment