Last active
June 18, 2020 15:27
-
-
Save roman204/34620424ac62e5db094fa0958d820ead to your computer and use it in GitHub Desktop.
handle the iframe height via postMessage
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
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