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

JavaScript: TypeError: xyz is not a function after upgrading WordPress

programmeradmin0浏览0评论

After upgrading WordPress from 4.9.13 to 5.3.2 some scripts on website stop working in weird way... (example for clndr.js).

All scripts are added via wp_register_script and wp_add_inline_script WordPress' functions.

At bottom of <body> there are three scripts as always:

<script type='text/javascript' src='.min.js'></script>
<script type='text/javascript' src='.js'></script>
<script type='text/javascript'>
    var calendar = create_calendar();
</script>

Relevant part of calendar.js

function create_calendar()
{
    var calendar = $('#Calendar').clndr({
        // clndr options
    });

    return calendar;
}

All scripts should run in right order, but they are not working and in browser console I have this error: TypeError: $(...).clndr is not a function

Also, changing call to create_calendar in this way:

window.addEventListener("load", function(){
    var calendar = create_calendar();
});

changes only order of errors in console.

If I understand everything correctly, this should just work.. Any ideas? Or hints how to debug this?

After upgrading WordPress from 4.9.13 to 5.3.2 some scripts on website stop working in weird way... (example for clndr.js).

All scripts are added via wp_register_script and wp_add_inline_script WordPress' functions.

At bottom of <body> there are three scripts as always:

<script type='text/javascript' src='http://example/clndr.min.js'></script>
<script type='text/javascript' src='http://example/calendar.js'></script>
<script type='text/javascript'>
    var calendar = create_calendar();
</script>

Relevant part of calendar.js

function create_calendar()
{
    var calendar = $('#Calendar').clndr({
        // clndr options
    });

    return calendar;
}

All scripts should run in right order, but they are not working and in browser console I have this error: TypeError: $(...).clndr is not a function

Also, changing call to create_calendar in this way:

window.addEventListener("load", function(){
    var calendar = create_calendar();
});

changes only order of errors in console.

If I understand everything correctly, this should just work.. Any ideas? Or hints how to debug this?

Share Improve this question edited Feb 16, 2020 at 16:52 acinis asked Feb 16, 2020 at 15:51 acinisacinis 12 bronze badges 2
  • jQuery isn't available as $ in WordPress. See wordpress.stackexchange/questions/2895/… – fuxia Commented Feb 16, 2020 at 15:55
  • Nice hint. I have custom jQuery included from beginning. Removing WordPress' one as in stackoverflow/a/27048128 fix all problems... They must change something, maybe naming of jQuery script handle.. – acinis Commented Feb 16, 2020 at 16:08
Add a comment  | 

1 Answer 1

Reset to default -1

According to @fuxia comment:

Solution is removing jQuery included by WordPress just before register/enqueue custom jQuery (see https://stackoverflow/a/27048128).

So if you include some jQuery on your own, then add

wp_dequeue_script('jquery');
wp_deregister_script('jquery');

just before register/enqueue custom jQuery script in wp_enqueue_scripts action.

Or if you want jQuery included by WordPress just remember, that it's in no-conflict mode and $ is undefined. See https://wordpress.stackexchange/a/2896/182809 for more details.

发布评论

评论列表(0)

  1. 暂无评论