Skip to content

Instantly share code, notes, and snippets.

@msurguy
Forked from mouse0270/Dynamic Iframe - Loop
Last active August 29, 2015 14:18
Show Gist options
  • Save msurguy/5b79e81e9590bf7683ad to your computer and use it in GitHub Desktop.
Save msurguy/5b79e81e9590bf7683ad to your computer and use it in GitHub Desktop.
// Use this if you believe the iframe may have dynamic content and could change size after it has completely loaded //
// Your Domain Name //
var parent_domain = "http://www.example.com/";
setInterval(function() {
var iframe_height = parseInt($('html').height());
// Add Padding to insure nothing is creating a scroll bar //
iframe_height += 30;
// Pass Page Height back to main page //
parent.postMessage( iframe_height, parent_domain );
}, 300 );
// This script goes on the page that contains the iframe, not the page that is being iframed //
// Create Listener for iframe //
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent",
eventer = window[eventMethod],
messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
// Listen for new iframe Height //
eventer(messageEvent,function(e) {
$('iframe#dynamic_iframe_preview').css('height', e.data+'px');
},false);
$("iframe#dynamic_iframe_preview").load(function() {
$("iframe#dynamic_iframe_preview").css('height', $("iframe#dynamic_iframe_preview").contents().find("html").height());
});
$(window).resize(function() {
$("iframe#dynamic_iframe_preview").css('height', $("iframe#dynamic_iframe_preview").contents().find("html").height());
});
// Use this script if you only want to update the iframe height once the iframe has fully loaded //
// Your Domain Name & iframe Height //
var parent_domain = "http://www.example.com/",
iframe_height = parseInt($('html').height());
// Add Padding to insure nothing is creating a scroll bar //
iframe_height += 30;
// Pass Page Height back to main page //
parent.postMessage( iframe_height, parent_domain );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment