From 132a98be68ae91a85bf0ceb0d0988974b942e1a5 Mon Sep 17 00:00:00 2001 From: Michal Kleiner Date: Tue, 21 Nov 2017 06:58:24 +1300 Subject: [PATCH] Support async script loading Use addEventListener to schedule BN initialisation only if document is still loading (#34) --- javascript/betternavigator.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/javascript/betternavigator.js b/javascript/betternavigator.js index 38191c9..e821846 100755 --- a/javascript/betternavigator.js +++ b/javascript/betternavigator.js @@ -32,13 +32,13 @@ function initialiseBetterNavigator() { } } -if (document.addEventListener) { +if (document.addEventListener && document.readyState === 'loading') { document.addEventListener("DOMContentLoaded", function () { - //wait til DOM is ready + // wait til DOM finishes loading initialiseBetterNavigator(); }); } else { - //This is the case for IE8 and below - //initialise straight away - fine if script is loaded after BN dom element + // This is the case for IE8 and below OR already loaded document (e.g. when using async) + // initialise straight away - fine if script is loaded after BN dom element initialiseBetterNavigator(); }