Skip to content

Instantly share code, notes, and snippets.

@roman204
Last active June 18, 2020 15:27
Show Gist options
  • Save roman204/34620424ac62e5db094fa0958d820ead to your computer and use it in GitHub Desktop.
Save roman204/34620424ac62e5db094fa0958d820ead to your computer and use it in GitHub Desktop.
handle the iframe height via postMessage
define([
'jquery',
], function ($) {
// modified from same-domain version at www.dyn-web.com/tutorials/iframes/height/
function setIframeHeightCO(id, ht) {
var ifrm = document.getElementById(id);
// some IE versions need a bit added or scrollbar appears
ifrm.style.height = ht + 4 + "px";
}
// iframed document sends its height using postMessage
function handleDocHeightMsg(e) {
// check origin
if ( e.origin === 'originName' ) {
// parse data
var data = JSON.parse( e.data );
// check data object
if ( data['docHeight'] ) {
setIframeHeightCO( 'configuratorFrame', data['docHeight'] );
}
}
}
// assign message handler
if ( window.addEventListener ) {
window.addEventListener('message', handleDocHeightMsg, false);
} else if ( window.attachEvent ) { // ie8
window.attachEvent('onmessage', handleDocHeightMsg);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment