HTML and CSS Reference
In-Depth Information
proclaimed utility-belt for JavaScript, Underscore calls itself “ … the tie to go along with jQuery's tux.” It uses
many of the same idioms as jQuery and is also namespace-friendly.
Do You Even Need a Library?
Technically, you could get by without any sort of JavaScript library. None of what you're going to use jQuery or
Underscore for is impossible to do with JavaScript directly. It just happens to be a lot less painful. Using
JavaScript, you would end up with longer, less understandable code and need to write a lot more branching state-
ments to handle different browser implementations.
Starting with jQuery
If you are already comfortable with jQuery, you may be tempted to skip this section. However, at least take a
quick glance because this section covers jQuery from a game-specific angle.
Adding jQuery to Your Page
The jQuery library consists of a single JavaScript file. To load jQuery on your page, you need to load this file
via a <script> tag. You have two options for doing this. You can download jQuery directly, or you can load
it via a Content Delivery Network (CDN).
Loading jQuery directly means that you have complete control over the file and the page. This is both a good
and a bad thing. jQuery is a large library, and if it's not served properly (minified and compressed), it can be a
beast. jQuery 1.7 clocked in at nearly 250 kb; if served compressed and minified, however, jQuery is only 33
kb. If you are serving it directly off your web server you need to make sure you are serving a minified version
with its cache headers set correctly, so the code isn't sent on subsequent reloads. (See the next chapter for more
details on cache headers.)
Another advantage of using a CDN is that most CDNs have edge-locations around the world, meaning
wherever in the world your game players are located, they will be near a CDN server location with a fast con-
nection to the Internet. This isn't something that you can always guarantee when serving files off a standard
web server.
One last advantage of using a CDN is that, given the ubiquity of jQuery, there's a good chance that visitors
already have a CDN version of jQuery cached in their browser, meaning 0 bytes need to be sent.
To serve jQuery off the Google CDN, all you need is to add a single <script> tag to the page:
<script
src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/
jquery.min.js'>
</script>
The jQuery website ( http://docs.jquery.com/Downloading_jQuery#CDN_Hosted_jQuery ) has a number of
options but the one hosted by Google is by far the most popular. To serve it locally, you need to download the
file from http://jquery.com and stick it somewhere you can access it:
<script src='js/jquery.min.js'></script>
Search WWH ::




Custom Search