Skip to content

Instantly share code, notes, and snippets.

@jgmuchiri
Last active January 3, 2019 21:42
Show Gist options
  • Save jgmuchiri/06c55465bf57c776687e13854027429a to your computer and use it in GitHub Desktop.
Save jgmuchiri/06c55465bf57c776687e13854027429a to your computer and use it in GitHub Desktop.
Resizes iframe if window height resizes
<iframe
src="your-url-here"
id="my_iframe"
style="width: 100%;"
border="0"
margin="0" padding="0"
frameborder="0" scrolling="no" ></iframe>
(function($, window, undefined){"use strict";
var frameId = "my_iframe";
var minHeight = 500;
var iframeHeight = 0;
var threshold = 5;
var frame = $("#"+frameId);
$(window).on("message", function(e) {
var data = e.originalEvent.data;
if (!data.match(/^HEIGHT:/)) {return true;}
var h = parseInt(data.substring("HEIGHT:".length), 10);
if (h < minHeight) h = minHeight;
if (Math.abs(iframeHeight - h) > threshold) {
iframeHeight = h;
frame.height(iframeHeight);
}
});
})(this.jQuery, this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment