Check if Bootstrap Datepicker script has loaded

2019-06-17 10:59发布

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

2条回答
疯言疯语
2楼-- · 2019-06-17 11:04

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>');                  
查看更多
该账号已被封号
3楼-- · 2019-06-17 11:14

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

查看更多
登录 后发表回答