0

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

console shows:

Uncaught ReferenceError: jQuery is not defined

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>
5
  • 1
    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 . – Anoop D Mar 14 at 11:57
  • 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 – Tom J Nowell Mar 14 at 12:23
  • @TomJNowell thanks a lot, you are my sunday hero :-) – Aprilia Mar 14 at 12:31
  • @AnoopD thanks a lot, you are my sunday hero :-) – Aprilia Mar 14 at 12:32
  • For reference, this did improve performance back in the 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 – Tom J Nowell Mar 14 at 12:34
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

0

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
1
  • FWIW, everything you're doing in your JavaScript is perfectly possible without jQuery, so I would recommend just avoiding the issue entirely by using 'vanilla' JavaScript. – Jacob Peattie Mar 14 at 14:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.