From 588f096afe07541f50c8025ae6730b835027d9f5 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Thu, 29 Apr 2021 22:00:15 +0200 Subject: [PATCH] Fix iframe for back button in Edge Chromium When navigating to the previous page in Edge Chromium the H5P would often break due to it not being loaded when the page is ready. This fix will force a reload of the iframe if it is broken on initialization. --- js/h5p.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/js/h5p.js b/js/h5p.js index a7096e7..05cd853 100644 --- a/js/h5p.js +++ b/js/h5p.js @@ -377,13 +377,29 @@ H5P.init = function (target) { // Insert H5Ps that should be in iframes. H5P.jQuery('iframe.h5p-iframe:not(.h5p-initialized)', target).each(function () { - var contentId = H5P.jQuery(this).addClass('h5p-initialized').data('content-id'); + const iframe = this; + const $iframe = $(iframe); + + const contentId = $iframe.data('content-id'); const contentData = H5PIntegration.contents['cid-' + contentId]; - const language = contentData && contentData.metadata && contentData.metadata.defaultLanguage + const contentLanguage = contentData && contentData.metadata && contentData.metadata.defaultLanguage ? contentData.metadata.defaultLanguage : 'en'; - this.contentDocument.open(); - this.contentDocument.write('' + H5P.getHeadTags(contentId) + '
'); - this.contentDocument.close(); + + const writeDocument = function () { + iframe.contentDocument.open(); + iframe.contentDocument.write('' + H5P.getHeadTags(contentId) + '
'); + iframe.contentDocument.close(); + }; + + $iframe.addClass('h5p-initialized') + if (iframe.contentDocument === null) { + // In some Edge cases the iframe isn't always loaded when the page is ready. + $iframe.on('load', writeDocument); + $iframe.attr('src', 'about:blank'); + } + else { + writeDocument(); + } }); };