Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 240
Matt Avatar asked Jul 21 '11 00:07

Matt


4 Answers

  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.
like image 75
Seth Avatar answered Sep 20 '22 00:09

Seth


When Javascript code is executing in your browser, all of your included Javascript files and any code you write in-between those "script" tags in the HTML document is going to be executed as though it were all part of one giant file (same namespace). So in some sense, it doesn't matter whether you write your code in the HTML document or whether you write it in an external file that you include - you're free to do either, and it will be executed the same. You can balance maintainability, reusability and convenience (think about what functions you write that you might want to reuse on other pages) and do whichever you feel is best.

To make this concrete - this is one valid way to write your Javascript, if you wanted to write the code inside your HTML file:

<html>
  <head>
    <script type="text/javascript" src="jquery.js"></script> 
    <script type="text/javascript">
      $(document).ready(function(){
        alert('Document Ready!');
      });
    </script>
  </head>
  <body>
  ...

Here's the intro at the jQuery site, for reference: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery

Writing your Javascript code at the bottom of the HTML page was/is a technique for getting it to execute as soon as the document is loaded, which is unnecessary when using jQuery's '$(document).ready' (that's what it does - it abstracts the business of getting Javascript functions to execute on page load, and implements it in a cross-browser way).

See: Introducing $(document).ready() for more.

like image 28
Elias Peterson Avatar answered Sep 22 '22 00:09

Elias Peterson


It doesn't really matter where you place your jQuery code. If you place it in the head tag, it'll automatically load everything. If you decide to place it all in an external JavaScript file, you need to link it with a <script type="text/javascript" src="my_file.js"></script> tag.

The 'code here' part is only for JavaScript. What the code is saying is that when the document is ready, run this function. The function can be whatever you like - whatever you put inside the function will run when the document is ready (i.e. when the webpage is called by the browser).

You don't need to insert it at the bottom of the HTML page - you can do it anywhere. People only insert it at the bottom to optimize their loading speed. It's nonessential.

like image 29
hohner Avatar answered Sep 18 '22 00:09

hohner


$(document).ready(function(){
    //Code here
});

goes in your javascript file. All javascript code that should be executed once the page has loaded goes where the //Code here comment is.

Perhaps a quick jQuery tutorial would be in order?

like image 31
Tomm Avatar answered Sep 19 '22 00:09

Tomm