Skip to content

Instantly share code, notes, and snippets.

@timramseyjr
Last active October 5, 2016 19:46
Show Gist options
  • Save timramseyjr/7d660e8fddf4d3e808b081d49706bbcd to your computer and use it in GitHub Desktop.
Save timramseyjr/7d660e8fddf4d3e808b081d49706bbcd to your computer and use it in GitHub Desktop.
Dynamic Iframe Height
<!--This code goes on iframe parent-->
<script>
/*Cross Domain Iframe Height*/
window.addEventListener('message', function(e) {
var $iframe = $("#trackframe");
var eventName = e.data[0];
var data = e.data[1];
switch (eventName) {
case 'setHeight':
$iframe.height(data + 20);
break;
}
}, false);
</script>
<iframe frameborder="0" width="100%" src="track-iframe.php" id="trackframe" name="trackframe"></iframe>
<!--This code goes on track-iframe.php -->
<script>
/*To Resize Iframe cross domain*/
function resize() {
var height = document.getElementsByTagName("html")[0].scrollHeight;
window.parent.postMessage(["setHeight", height], "*");
}
document.addEventListener("DOMContentLoaded", function() {
resize();
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment