From a4709f5b91aaafd13b69102c8826f1d8721411d8 Mon Sep 17 00:00:00 2001 From: Mateusz Uzdowski Date: Fri, 9 Aug 2019 11:16:41 +1200 Subject: [PATCH] Update for jQuery 3. --- javascript/iframe_page.js | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/javascript/iframe_page.js b/javascript/iframe_page.js index e0ab968..c3629ef 100644 --- a/javascript/iframe_page.js +++ b/javascript/iframe_page.js @@ -9,26 +9,27 @@ if (typeof jQuery!=='undefined') { iframe.hide(); loading.show(); - $(function() { - $( window ).load(function() { - // Iframe content has been loaded - loading.hide(); - iframe.show(); + $( iframe ).on('load', function() { + // Iframe content has been loaded + loading.hide(); + iframe.show(); - if (iframe.hasClass('iframepage-height-auto')===true) { - // Try to set the height to the height of the iframe content (only possible if it is the same domain) - try { - var iframeInsideSize = $(iframe[0].contentWindow.document.body).height(); - iframe.css('height', (parseInt(iframeInsideSize)+100)+'px'); - } - catch(e) { - // Failed to set the height, we fall back to default css height. - } + if (iframe.hasClass('iframepage-height-auto')===true) { + // Try to set the height to the height of the iframe content (only possible if it is the same domain) + try { + // Use plain JS to get iframe size. There is some timing issue with jQuery + // which causes the iframe size to be 0 even after iframe.show(). + const domframe = document.querySelector('#Iframepage-iframe'); + var iframeInsideSize = domframe.contentWindow.document.body.offsetHeight; + iframe.css('height', (parseInt(iframeInsideSize)+100)+'px'); } + catch(e) { + // Failed to set the height, we fall back to default css height. + } + } - // Finally, remove the marker - iframe.removeClass('iframepage-not-ready'); - }); + // Finally, remove the marker + iframe.removeClass('iframepage-not-ready'); }); })(jQuery); }