Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - some beginner questions

I'm a very beginner to jQuery, and I'm having some basic questions:

  1. Is it advisable to use jQuery whenever it is possible to replace something by using it? For example, is it prudent to bind all events to elements using it, instead of through HTML?

  2. Is it better to host the jQuery .js file and all other relevant files (like JQuery UI) myself, or is it perhaps a better choice to use Google's link (they seem to host it for others too)?

  3. When it comes to executing a script when the page is done loading, what way is preferred?

    1. $(document).ready(function() {})
    2. $(function() {})
    3. $().ready(function() {})

    They seem to all do the same thing, but what is the preferred way of scripting?

like image 591
pimvdb Avatar asked Jan 18 '11 14:01

pimvdb


1 Answers

  1. Yes. This way your JS is cleanly separated from your html. You can look at your file and in one glance, see how it is affecting your HTML. If it was embedded in the HTML, you would have to look for onClick, onLoad etc and it can get pretty messy for large applications.

  2. Client browsers will cache files, so if you use the google version of JQuery, it will not have to download it off your server. Saving you bandwidth.

  3. $(document).ready(function() {}) Is the preferred choice. $(function() {}) Just defines the block for execution, it will only execute after the page is ready if it is the last thing on the page to get executed.

like image 59
Chris Kooken Avatar answered Sep 28 '22 22:09

Chris Kooken