My site is built with WordPress, and recently I’ve been adding some basic javascript to it: RossPW.com
However, all the javascript I’ve added doesn’t seem to work, and I can’t figure out why for the life of me!
For example, I added the following simple snippet to the header, to fade in the — but this doesn’t work:
<script type="text/javascript">
$('body').hide();
$('body').fadeIn(3000);
</script>
To try and troubleshoot this so far, I have tried two things:
1) Properly added wp_enqueue to the header.php (this has been known to cause some problems with wordpress in the past:
<?php wp_enqueue_script("jquery"); ?>
<?php wp_head(); ?>
2)I also tried included javascript as an external .js file here, with some basic JS to animate the header or scroll to the top — this hasn’t worked either.
Any help would be much appreciated — thanks!
UPDATE:
In order to ensure that js/jquery are loaded properly. I’ve tried this basic alert — which does in fact work!
<script type="text/javascript">
alert('ALERT!')
</script>
However, all the other javascript I’ve written does not — and I can’t figure out why. The javascript I’ve written seems to be fine, as seen here: jsfiddle.net/v9NSR/
Two Things to fix –
http://rosspw.com/wp-content/themes/rosspw2013/js/rosspw.js Line: 21,
Column: 5 Source Code: });â ;ââ¬â¹
Let me know if it works for you or not.
Try to use
Firebug
for debugging.For better Understanding Check this screenshot –
As you are using jquery before its been loaded.
The first thing you had wrong in your code was using the
$
sign to call jQuery. When you are coding jQuery in WordPress, you have to make sure there is no conflict with the code libraries as WordPress may use other libraries and it might mess up.The proper way to use jQuery in WordPress is
jQuery();
instead of$();
which in your case would be:Another way to do it would be to wrap the jQuery code with the jQuery
$.noConflict()
function, such as:Check this out for more information and full documentation about the noConflict function.
Hope it works for you!
It looks like you are calling your jquery functions before the script tag that is loading jquery.js.
Line 70-71:
This gives an exception:
Uncaught ReferenceError: $ is not defined
But jquery.js isn’t loaded until line 94:
You can do one of two things:
Call your jquery functions after the document has been loaded. Something like:
function fadeInBody() {
$(‘body’).hide();
$(‘body’).fadeIn(3000);
}
window.onload = fadeInBody;
try adding jquery on the footer just before
wp_footer()
followed by your scriptthen on you external script: