Support async script loading

Use addEventListener to schedule BN initialisation only if document is still loading (#37)
This commit is contained in:
Michal Kleiner 2017-11-21 12:04:20 +13:00 committed by Jono Menz
parent 63f16e4054
commit 5a0b917874
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 () {
//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();
}