From c84bdc78c829d28968dd25fef9e0c67a90a91bd1 Mon Sep 17 00:00:00 2001 From: Steven D Vachon Date: Tue, 8 Aug 2017 14:20:43 -0400 Subject: [PATCH] Add support for loading before DOMContentLoaded fixes #33 --- date-input-polyfill.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/date-input-polyfill.js b/date-input-polyfill.js index f6ec649..c67fb19 100644 --- a/date-input-polyfill.js +++ b/date-input-polyfill.js @@ -9,15 +9,20 @@ const addPickers = () => { } }; -// Run the above code on any in the document, also on dynamically created ones. -addPickers(); - -document.addEventListener(`DOMContentLoaded`, () => { +const init = () => { + // Run the above code on any in the document, also on dynamically created ones. addPickers(); -}); + // This is also on mousedown event so it will capture new inputs that might + // be added to the DOM dynamically. + document.querySelector(`body`).addEventListener(`mousedown`, () => { + addPickers(); + }); +}; -// This is also on mousedown event so it will capture new inputs that might -// be added to the DOM dynamically. -document.querySelector(`body`).addEventListener(`mousedown`, () => { - addPickers(); -}); +if (document.readyState !== `loading`) { + init(); +} else { + document.addEventListener(`DOMContentLoaded`, () => { + init(); + }); +}