Support async script loading

Use addEventListener to schedule BN initialisation only if document is still loading (#34)
This commit is contained in:
Michal Kleiner 2017-11-21 06:58:24 +13:00 committed by Jono Menz
parent 2a9df8695d
commit 132a98be68
1 changed files with 4 additions and 4 deletions

View File

@ -32,13 +32,13 @@ function initialiseBetterNavigator() {
} }
} }
if (document.addEventListener) { if (document.addEventListener && document.readyState === 'loading') {
document.addEventListener("DOMContentLoaded", function () { document.addEventListener("DOMContentLoaded", function () {
//wait til DOM is ready // wait til DOM finishes loading
initialiseBetterNavigator(); initialiseBetterNavigator();
}); });
} else { } else {
//This is the case for IE8 and below // 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 // initialise straight away - fine if script is loaded after BN dom element
initialiseBetterNavigator(); initialiseBetterNavigator();
} }