function findSourceIframe(windowObj) { var iframes = window.document.querySelectorAll('iframe'); for(var i = 0; i < iframes.length; ++i) { if(windowObj === iframes[i].contentWindow) { return iframes[i]; } } return null; } function receiveMessage(e) { var _widthCorrection = (typeof widthCorrection == 'undefined' ? 0 : widthCorrection); var _heightCorrection = (typeof heightCorrection == 'undefined' ? 0 : heightCorrection); var origin = e.origin || e.originalEvent.origin; if (typeof allowedOrigin !== 'undefined' && origin !== allowedOrigin) return; var iframe = findSourceIframe(e.source.window); if(iframe) { iframe.style.height = (e.data.contentHeight + _heightCorrection) + 'px'; iframe.style.width = (e.data.contentWidth + _widthCorrection) + 'px'; } } window.addEventListener('message', receiveMessage, false);