0

I have the following code to load an external script, I can see in the console network tab the script gets loaded but I get error:

Uncaught TypeError: $(...).pikaday is not a function

This is my script:

<input class="my-datepicker" id="from_date" type="text" placeholder="From" name="from_date" value="">

<script>

$(window).on('load', function() {

   $.when(
      $.getScript( "https://cdnjs.cloudflare.com/ajax/libs/pikaday/1.8.0/pikaday.min.js" ),
      $.Deferred(function( deferred ){
          $( deferred.resolve );
      })
   ).done(function(){

      $(".my-datepicker").pikaday({
         firstDay: 1
      });

  })

});

</script>
adam78
  • 8,002
  • 17
  • 68
  • 154
  • [`$.getScript()`](http://api.jquery.com/jquery.getscript/): _"The callback is fired once the script has been loaded **but not necessarily executed**"_ – Andreas May 15 '19 at 09:17
  • And why `$.Deferred(function( deferred ){ $( deferred.resolve ); })`? – Andreas May 15 '19 at 09:20
  • @Andreas so how do i fix this? – adam78 May 15 '19 at 09:21
  • @Andreas see the following answer for details on why `$.Deffered` https://stackoverflow.com/questions/11803215/how-to-include-multiple-js-files-using-jquery-getscript-method – adam78 May 15 '19 at 09:23
  • _"...because of the $.Deferred call which resolves in the DOM ready callback."_ - Your script is executed way after `DOMReady` ([window.onload vs $(document).ready()](https://stackoverflow.com/questions/3698200/window-onload-vs-document-ready)), hence that promise/deferred will be resolved immediately and is therefor useless. – Andreas May 15 '19 at 12:07
  • @andreas very well but you haven't provided any alternative solution to fix the issue at hand. – adam78 May 16 '19 at 21:13

0 Answers0