Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
962 views
in Technique[技术] by (71.8m points)

plugins - Missing jQuery in a Wordpress widget

Using Astra theme, I cannot use jQuery on the frontend. It keeps logging Uncaught ReferenceError: jQuery is not defined at VM105:23. It is actually supposed that Wordpress is equipped with jQuery, but I cannot figure out why I get that 'undefined' error message in console.

So as to add jQuery, I just write the following code, yet to no effect:

    function addjq () {
      wp_enqueue_script('jquery', plugin_dir_url(__FILE__) . '/in/jq.js', array('jquery'), null, false);
    }
    add_action('wp_enqueue_scripts', 'addjq');

I am using this piece inside a widget, and it is not including jquery to the frontend. It actually comes up with that Uncaught ReferenceError. Is the problem somehow related to my jquery file url, or is it a core problem that I need to address. Can you help me out of the problem, and let me know how I can use jquery inside a plugin widget. The following jQuery code throws that console error:

jQuery(document).ready(function() {
   // Some Code Goes Here...
  })

I also tried to add a src attribute to the script tag, and bring in the jQuery. RESULT: The error disappeared, yet no jQuery code was run. Even I could not do a simple console.log().

Thanks in advance...

question from:https://stackoverflow.com/questions/65647424/missing-jquery-in-a-wordpress-widget

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem was actually with the jQuery code I had written.

    jQuery(document).ready(function() {
      // Some Code Goes Here...
    })

Just take a look at the complete segment:

    public function doSomethingSpecific() {
        ?>
            <script type="text/javascript" >
                              
                    jQuery(document).ready(function() {
                      console.log('Hello World!');
                    })

                }

            </script>
        <?php
    }

Since the above function was not correctly enqueued, and actually supposedly kept running before jQuery was loaded, I received that Uncaught ReferenceError: jQuery is not defined at VM105:23 error. So what I did to solve the problem was to wait for the page to load fully and then call the code. Therefore I changed that jQuery segment shown above to the following and it was good to go. Problem Solved, and definitely there was no need to enqueue 'jQuery'.

    public function webcamcapchur() {
        ?>
            <script type="text/javascript" >
                window.onload = function doLoadThePage(event) {
                    jQuery(document).ready(function() {
                      console.log('Hello World!');
                    })
                }

            </script>
        <?php
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...