brianblakely / nodep-date-input-polyfill

Automatically adds datepickers to input[type=date] on IE, macOS Safari, and legacy browsers.
https://www.npmjs.com/package/nodep-date-input-polyfill
MIT License
96 stars 137 forks source link

Initialization skipped if script loaded dynamically #27

Closed halovanic closed 7 years ago

halovanic commented 8 years ago

The initialization of this script assumes the document.DomContentLoaded or window.load events will fire after it has been loaded, never before. I think this is supposed to be true if this is included as a <script> element in the HTML (barring old browsers), but it does not work if this script is dynamically added, such as with WebPack chunks, AMD or other loader mechanisms. This is useful if you want to only include the polyfill where you actually have date input types.

The fix is very basic, just first check if the document is already loaded and either initialize right away or add the event listeners.