Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught ReferenceError: jQuery is not defined

I'm trying to put together a Wordpress site with a JQuery slider plugin, but the plugin doesn't display on the page and I always get the above error message.

Despite trying several different fixes suggested in other's posts I still can't seem to fix the above error, including putting the script tag in the 'header.php' file. Any help would be much appreciated - thanks!

Relevant code in 'footer.php' file

    <!--Load JQuery-->  
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>


</body> 
</html>

The Website:http://www.advanceprojects.com.au/

like image 325
user2541351 Avatar asked Oct 30 '25 15:10

user2541351


2 Answers

Scripts always run in a sequential order.

So basically you are trying to use jQuery even before the jQuery library is loaded.

Your script is dependent on Nivo which in turn depends on jQuery library.

Either move the script to the line after the library is loaded, or move the library declaration to the head.

Also make sure you enclose the script inside DOM Ready handler.

So the order in which you should be loading these are

-- jQuery 
  -- Nivo Slider
  -- your script that uses the above 2 libraries.
like image 96
Sushanth -- Avatar answered Nov 02 '25 23:11

Sushanth --


This worked for me.

Add the line

wp_enqueue_script('jquery');

This error happened because jquery is not included, and should be included before loading any other script. Here is an example:

firstplugin.php

<?php
/*
Plugin Name: Plugintrial
Description:  plugin to test jquery
Version:      1
Author:       Dismi Paul
*/
function childtheme_custom_login() {
wp_enqueue_script('jquery');

wp_enqueue_script('my-custom-script', plugins_url('/test.js', __FILE__));
?>

    <h1>Welcome to My Homepage</h1>

<p id="intro">My name is Donald.</p>
<p>I live in Duckburg.<?php echo plugins_url('/test.js', __FILE__); ?></p>

    <?php
}

add_action('login_head', 'childtheme_custom_login');

test.js

jQuery(document).ready(function()
{
  jQuery("#intro").css("background-color", "yellow");
});
console.log("it works");
like image 41
Dismi Paul Avatar answered Nov 03 '25 00:11

Dismi Paul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!