最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if Bootstrap Datepicker script has loaded - Stack Overflow

programmeradmin1浏览0评论

I am receiving the error:

Uncaught TypeError: undefined is not a function

When I try to set up the datepicker:

$('.datepicker').datepicker();

How can I determine if the datepicker has loaded or not?

To be clear, I'm not using jQuery UI

I am receiving the error:

Uncaught TypeError: undefined is not a function

When I try to set up the datepicker:

$('.datepicker').datepicker();

How can I determine if the datepicker has loaded or not?

To be clear, I'm not using jQuery UI

Share Improve this question edited Feb 15, 2015 at 23:47 Todd Ryler asked Feb 15, 2015 at 23:02 Todd RylerTodd Ryler 931 silver badge5 bronze badges 2
  • 1 You are missing jquery UI library. – Tomanow Commented Feb 15, 2015 at 23:02
  • @Tomanow Sorry for the confusion, I'm not using jQuery UI, I'm using bootstrap datepicker - bootstrap-datepicker.readthedocs.org/en/release – Todd Ryler Commented Feb 15, 2015 at 23:47
Add a comment  | 

2 Answers 2

Reset to default 23

You can check to see if the datepicker script has loaded using:

if ($.fn.datepicker) {
    // ...
}

Then you can conditionally invoke the .datepicker() method if the script has loaded:

if ($.fn.datepicker) {
    $('.datepicker').datepicker();
}

Example Here

In doing so, the error won't be thrown if the script hasn't loaded.


You can also use:

if (typeof $().datepicker === 'function') {
    $('.datepicker').datepicker();
}

Example Here

This stopped me loading bootstrap-datepicker more than once throughout modals..

$.isFunction(Date.today) || $('body').append('<script src="assets/bootstrap-daterangepicker/date.js"><\/script><script src="assets/bootstrap-daterangepicker/daterangepicker.js"><\/script>');                  
发布评论

评论列表(0)

  1. 暂无评论