03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

An Introduction to jQuery x 391<br />

How It Works<br />

To see how it works, take a look at the Scripts folder <strong>in</strong> your<br />

Solution Explorer. You should see an item called jquery-1.7.2.js,<br />

shown <strong>in</strong> Figure 11-3. Remember, a new version may have been<br />

released s<strong>in</strong>ce this book was written so your version numbers may<br />

be different. Dur<strong>in</strong>g <strong>in</strong>stallation, NuGet added three jQuery-related<br />

JavaScript files to your Scripts folder (which it would have created<br />

if it didn’t exist.) In addition, it kept track of the <strong>in</strong>stalled packages<br />

<strong>in</strong> a file called packages.config that has been placed <strong>in</strong> the root of<br />

your website <strong>and</strong> created a packages folder to store a local copy of<br />

FIGURE 11-3<br />

the downloaded packages. Depend<strong>in</strong>g on your sett<strong>in</strong>gs, this location<br />

could differ, but most likely it’s located <strong>in</strong> a folder named after your site <strong>in</strong> Documents\Visual Studio<br />

2012\Projects.<br />

These three .js files all serve a different purpose. The file jquery-1.7.2.js conta<strong>in</strong>s the core jQuery<br />

code <strong>in</strong> an uncompressed format. This means you can read its code <strong>and</strong> use it for debugg<strong>in</strong>g purposes<br />

<strong>in</strong> cases where you need to see what code the library executes. The file jquery-1.7.2.<strong>in</strong>tellisense<br />

.js conta<strong>in</strong>s the documentation for the code <strong>in</strong> the jQuery library that is used <strong>in</strong> the IntelliSense lists<br />

<strong>and</strong> documentation pop-ups. You see how this works later <strong>in</strong> this chapter. F<strong>in</strong>ally, the fi le jquery-<br />

1.7.2.m<strong>in</strong>.js is the core jQuery library <strong>in</strong> a compressed format. This is the file you’ll use <strong>in</strong> your website<br />

because it has the smallest size <strong>and</strong> is thus the quickest to download.<br />

Now that the jQuery library has been added to the website, the next step is determ<strong>in</strong><strong>in</strong>g where to<br />

use it. Because the jQuery library adds to the size of your web pages, it should be a deliberate choice<br />

whether or not you <strong>in</strong>clude it <strong>in</strong> your site.<br />

Choos<strong>in</strong>g the Location for Your jQuery Reference<br />

To <strong>in</strong>clude jQuery <strong>in</strong> your website, you have a couple of options:<br />

‰ Add a reference to the jQuery library <strong>in</strong> just the web pages or user controls that require it.<br />

‰ Add a reference to the jQuery library <strong>in</strong> the master page of your site so it’s available <strong>in</strong><br />

all pages.<br />

Both methods have their own advantages <strong>and</strong> disadvantages. Add<strong>in</strong>g a reference to the jQuery<br />

library <strong>in</strong> just the pages that need it helps keep the size of your pages down a bit. When your users<br />

browse only to pages without jQuery, they’ll never have to download the library file, sav<strong>in</strong>g them<br />

some b<strong>and</strong>width. Note that once they’ve visited a page that does reference the library, the browser<br />

will cache a copy of it, remov<strong>in</strong>g the need to download it aga<strong>in</strong> on subsequent visits to pages.<br />

Add<strong>in</strong>g the reference to jQuery <strong>in</strong> the master page of your site is quite convenient, because all pages<br />

based on this master page automatically get access to the jQuery functionality. However, this results<br />

<strong>in</strong> a small performance hit on the first page of your site because the library needs to be downloaded<br />

from the server. This is the option used <strong>in</strong> the Planet Wrox website.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!