Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$(document).ready not firing?

Tags:

jquery

Such simple code, why isn't it working? When the page loads, it should display an alert box that reads "ready".

<!DOCTYPE html>
<html>
    <head>
        <title>
        Title
        </title>
        <script type="text/javascript">
            $(document).ready(function() {
                alert("ready");
            });
        </script>
    </head>
    <body>
        Content
    </body>
</html>

I feel like it's something incredibly obvious, but I'm at a point where I can't think straight.

I've tried both in the latest versions of Chrome and Firefox.

like image 441
Hanna Avatar asked Jun 30 '11 03:06

Hanna


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 difference between document ready and document load?

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 does .ready do in JavaScript?

The ready() method is used to make a function available after the document is loaded. Whatever code you write inside the $(document ). ready() method will run once the page DOM is ready to execute JavaScript code.

What is the difference between the $( document .ready and window onload events?

The $(document). ready() is a jQuery event which occurs when the HTML document has been fully loaded, while the window. onload event occurs later, when everything including images on the page loaded.


3 Answers

Where's your jquery ref?

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
like image 111
orkutWasNotSoBad Avatar answered Oct 20 '22 22:10

orkutWasNotSoBad


I had this problem, but jQuery was referenced. I had accidentally written the previous script tag as a self closing tag when moving my javascript to an external file:

<script type="text/javascript" src="/wherever/whatever.js" />
<script type="text/javascript">
    $(document).ready(function(){ /* not hit */ });
</script>

The external reference tag can't be self-closing. It should read <script type="text/javascript" src="/wherever/whatever.js"></script>

like image 32
xr280xr Avatar answered Oct 20 '22 20:10

xr280xr


You forgot to include jQuery, so $ is undefined.

One glance at the JS console in chrome was all it took to figure out. Whenever something JS wont go, your first check should always be the console to look for an error. Usually this will tell you exaxtly what's up.

like image 29
Alex Wayne Avatar answered Oct 20 '22 22:10

Alex Wayne