Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is $(document).ready not firing for me?

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?

like image 893
never_had_a_name Avatar asked Dec 06 '09 23:12

never_had_a_name


People also ask

What does $( document .ready function () do?

$( document ). ready()A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ). ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

What is $( document .ready () and $( window .load () in jQuery?

ready() and $(window). load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc. are loaded, but after the whole DOM itself is ready.

What is difference between $( document .ready function () vs $( function ()?

So technically they are both the same. Not major difference between these two declaration. They used based on weather you use JavaScript then you should use $(document). ready declaration in other case you use jQuery library which is a part of JavaScript then you should use $(function) declaration.

Can you have multiple $( document .ready function ()?

ready' function in a page? Can we add more than one 'document. ready' function in a page? Yes we can do it as like I did in below example both the $(document).


2 Answers

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"></script> 

Chances are, this is missing or typed incorrectly.

like image 188
David Avatar answered Sep 28 '22 02:09

David


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.

like image 27
2 revs Avatar answered Sep 28 '22 02:09

2 revs