Search code examples
javascripthtmljquery

Where do I put the $(document).ready()?


I've been trying to add JavaScript to my HTML/CSS, but been running around in circles.

My current set-up is where the HTML, CSS, and JavaScript files (2 files; my JavaScript code, and jQuery's code) are all separate, but linked to each other via the HTML page.

So here are my questions:

  1. Do I put the link to the jQuery code within the HTML head? Or within my JavaScript code page?

  2. Where does this code go? The HTML page, or my JavaScript page?

    $(document).ready(function(){
      //Code here
    });
    
  3. Above, by 'code here', they mean JavaScript code, right? Not my html code?

  4. I've read about initializing JavaScript code at the bottom of an HTML page. From what I take though, I don't have to do that with jQuery's .ready function, right?


Solution

    1. You should like to your JavaScript files either in the <head> or above the closing </body> tag.
    2. The code can go anywhere really, but I would suggest an external JavaScript page.
    3. Yes
    4. This is correct.