views:

1460

answers:

5

In a php file i have used include to include the following js.php file and prior to that i have included the jquery file.

<script type="text/javascript">

$(document).ready(function(){
     alert("hello");
});
</script>

But it doesn't work. Why? when I skip the $(document).ready function it works.

But i need jquery code inside. what is wrong?

+2  A: 
  1. Check whether jQuery is loaded correctly.
  2. Look at the browser's progress bar: it may be loading some counters and the document is not ready until they're loaded: this often happens when external resources are slow.
  3. Try $(function(){ alert(...); }); just in case...
  4. Check whether you have JS errors prior to this onload binding. Use Firefox's FireBug plugin to check it out.
o_O Tync
firebug says that $(document).ready(function(){is not defined...is it something wrong with my jquery file?
never_had_a_name
Are you using Mootools?... if so, make sure you have Jquery set for compatibility
fudgey
if it says $(document).... is not a function that means that 1) another library is capturing the $, or that jQuery did not load properly.
contagious
for the next time: firebug -> network tab -> you'd see a 404 ...
Philippe Gerber
+6  A: 

The most likely answer, based on what you have said, is that the core jQuery file is not actually included correctly in the page. You need something like:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;

Chances are, this is missing or typed incorrectly.

David
i misspelled the jquery file name:P THANKS!
never_had_a_name
I have seen this same exact question numerous times - and it was nearly always the jQuery <script> tag like this occurance. Even done it myself :)
Mark Schultheiss
Using the Google loader can help avoid these kinds of typos, as well as automatically pulling down an updated version of jQuery (if desired):<script src="http://www.google.com/jsapi"></script><script> google.load("jquery", "1");</script>
Tim Ridgely
A: 

Is it possible you misspelled the name of the jquery file?

jeff
A: 

maybe his file is loaded before jquery?

JUGO
A: 

Another cause that will silently fail, and all remaining callbacks never called:

$(document).ready(null);

So check if you have variables or syntax errors that return null. Like this one:

$(document).ready(function($){}(jQuery));

Notice that the function above is called instantly and undefined is returned.

gregers