Uncaught ReferenceError: jQuery is not defined: how to fix on a sunday?

Can a non-javascript person fix this error? any advice? thank you for help?

console shows:

Uncaught ReferenceError: jQuery is not defined: how to fix on a sunday?

code is:

                        <script async src="https://cdn.filmpuls.info/jquery/jquery.min.js"></script>
                    <script type="text/javascript">
                        function insertBefore(e, t) {
                            t.parentNode.insertBefore(e, t),
                            e.classList.add("entry-content")
                        }
                        var intocontent = document.getElementsByClassName("intro")
                          , intocontentheading = document.getElementsByClassName("mh-meta");
                        insertBefore(intocontent[0], intocontentheading[0]);
                        var postId = "55687"
                          , postIdClass = ".post-" + postId;
                        console.log(postIdClass),
                        jQuery(document).ready(function() {
                            jQuery("body.single .mh-widget").each(function(e) {
                                jQuery(this).find(postIdClass).length && jQuery(this).find(postIdClass).hide()
                            })
                        });
                    </script>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It’s a very bad idea to deregister jQuery and re-register it to a CDN, and it does not improve performance. It can also cause issues due to mismatched jQuery versions. If you want to use jQuery on your site, just enqueue the jQuery that comes with WordPress.

As Anoop says:

You are loading jQuery async means, you are telling the browser, that’s it’s ok to load jQuery anytime, either early or later, so it makes sense that in some cases, it loads after page load and thus jQuery is undefined . Removing the async will make it work

So remove the script tag, and enqueue jQuery normally, and the issue should improve.


For reference, this did improve performance back in the early 2000’s, but the browser cache key now contains the domain so if you load the CDN on a new site you get a new copy, it doesn’t load the version it already downloaded ( which was the idea behind shared CDN improving performance ).

It also means HTTP2 optimisations can’t be used to transfer the file faster since it’s on a separate domain/server. Since it was a popular change it’s stuck around even though it doesn’t help anymore

Method 2

thanks to the inputs of two great members of this community, I could solve the matter without touching any code, applying the follow fixes:

  • exclude jquery.min.js from “CDN Enabler” plugin
  • exclude jquery.min.js in plugin “Async JavaScript”
  • exclude jquery.min.js in plugin “Autoptimize” in the section JavaScript-options


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x