Last active
August 29, 2015 14:05
-
-
Save ourmaninamsterdam/bbb53e02016f61f45341 to your computer and use it in GitHub Desktop.
Iframe Resizer (no dependencies) with AMD/global definitions
This file contains hidden or 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
(function (root, factory) { | |
if (typeof define === 'function' && define.amd) { | |
define('iframeResizer', function (iframeResizer) { | |
return (root.iframeResizer = factory()); | |
}); | |
} else { | |
root.iframeResizer = factory(); | |
} | |
}(this, function () { | |
return function(msgPrefix, targetOrigin, minFrameHeight,) { | |
var timer, frameDefaultOverflow; | |
minFrameHeight = minFrameHeight || 250; | |
targetOrigin = targetOrigin || '*'; | |
return { | |
start : function() { | |
if(!window.postMessage) return; | |
var currentHeight = 0, | |
bodyNode = document.body, | |
frameTopMargin, | |
frameBtmMargin; | |
frameDefaultOverflow = window.getComputedStyle(bodyNode).overflow; | |
document.documentElement.style.overflow = 'hidden'; | |
frameTopMargin = parseInt(window.getComputedStyle(bodyNode).marginTop, 16);; | |
frameBtmMargin = parseInt(window.getComputedStyle(bodyNode).marginBottom, 16) | |
timer = setInterval(function() { | |
var documentHeight = Math.max( | |
frameTopMargin + frameBtmMargin + bodyNode.clientHeight, | |
minFrameHeight | |
); | |
if(documentHeight !== currentHeight) { | |
window.parent.postMessage(msgPrefix + documentHeight, targetOrigin); | |
currentHeight = documentHeight; | |
} | |
}, 200); | |
}, | |
stop: function() { | |
clearInterval(timer); | |
document.documentElement.style.overflow = frameDefaultOverflow; | |
} | |
} | |
} | |
})); |
Author
ourmaninamsterdam
commented
Sep 2, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment