Last active
January 3, 2016 20:29
-
-
Save mouse0270/8514850 to your computer and use it in GitHub Desktop.
iframe's with dynamic heights
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$("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()); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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