最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Where to put all that jQuery JavaScript code? - Stack Overflow

programmeradmin7浏览0评论

From the documentation I've found this example:

We can animate any element, such as a simple image:

<div id="clickme">
  Click here
</div>
<img id="book" src="book.png" alt="" width="100" height="123" />

With the element initially shown, we can hide it slowly:

$('#clickme').click(function() {
  $('#book').fadeOut('slow', function() {
    // Animation plete.
  });
});

I remember from 5 years ago, that you should NEVER ever refer to any element until it was defined. Does this rule still apply? So I would have to put all that code in the footer of my web page? Or can I put it in a separate file and import it in the footer? What's best practice?

From the documentation I've found this example:

We can animate any element, such as a simple image:

<div id="clickme">
  Click here
</div>
<img id="book" src="book.png" alt="" width="100" height="123" />

With the element initially shown, we can hide it slowly:

$('#clickme').click(function() {
  $('#book').fadeOut('slow', function() {
    // Animation plete.
  });
});

I remember from 5 years ago, that you should NEVER ever refer to any element until it was defined. Does this rule still apply? So I would have to put all that code in the footer of my web page? Or can I put it in a separate file and import it in the footer? What's best practice?

Share Improve this question edited Oct 15, 2010 at 14:54 Felix Kling 817k180 gold badges1.1k silver badges1.2k bronze badges asked Oct 15, 2010 at 14:52 Proud MemberProud Member 40.5k48 gold badges152 silver badges232 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 8

The remended way of doing this is putting all initialization code into $(document).ready, like so:

$(document).ready(function() {
   $('#foobar').click(function(event) { alert("You Clicked Me!"); });
});

You are correct; you cannot interact with a DOM element before it exists.

You have two options:

  • Put the code below the HTML, as you suggested.

  • Put the code anywhere, but wrap it in $(function() { ... }).
    This construct will execute the function in the page load event, after the DOM exists.

The best practice is to place all SCRIPT elements at the bottom of the HTML document (right before the </body> tag. The reasons are:

  1. loading of external JS files blocks loading of other resources (like images)
  2. since JS code is executed immediately, it is better to parse the HTML code of the page first, and then execute the JS code afterwards

You can see an HTML5 template that demonstrates this practice here: http://vidasp/HTML5-template.html

Many people put it in the bottom of the page so other code can execute first. That bees a bit of a moot point with the document ready syntax that waits until other content loads to the dom. So, using that logic, in theory it could go anywhere.

Scripts go best in the foot of the page, to provide for the speediest rendering of the DOM. The following idiom executes only once the DOM is ready:

$(function() { /* ... your code goes here ... */ }

If you have a lot of code, or code that is shared between multiple pages, you should link it in a separate file which can then be minified, should you need to optimize your download speed.

I asked this question, albeit in a different way a little while back. You might want to look at the answers I got too - they're quite ... philosophical:

JQuery - Best way of wiring GUI objects to events?

发布评论

评论列表(0)

  1. 暂无评论