02.06.2013 Views

jQuery Cookbook - Cdn.oreilly.com - O'Reilly

jQuery Cookbook - Cdn.oreilly.com - O'Reilly

jQuery Cookbook - Cdn.oreilly.com - O'Reilly

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Documentation<br />

Tutorials<br />

<br />

<br />

<br />

We’ve removed the inline event handlers and replaced them with <strong>jQuery</strong> event handlers,<br />

separating the content and behavior. Now if we want to add more menu items,<br />

we don’t have to copy and paste the same batch of event handlers; instead, the event<br />

handler will automatically be added.<br />

We have also moved the style rules for the hover effect into a CSS class, separating the<br />

behavior and presentation. If we want to change the styling for the hover effect later,<br />

we can just update the stylesheet instead of having to modify the markup.<br />

Discussion<br />

While an “all in one” HTML file with onevent attributes works fine in a small, simple<br />

page, it doesn’t scale up very well. As your pages get more <strong>com</strong>plex, separating presentation<br />

and behavior makes the code easier to maintain.<br />

We didn’t do it in this simple example, but if you have multiple pages using the same<br />

JavaScript or CSS code, move that code to a <strong>com</strong>mon .js or .css file. That way it will<br />

be downloaded into the browser cache once, instead of being re-sent on every page<br />

load. As a result, once one of your pages has been visited, the rest will load faster.<br />

5.20 Using <strong>jQuery</strong> for Progressive Enhancement<br />

Problem<br />

You want to build a site that allows simple task management with a great user experience<br />

using animations and Ajax, but you also want to support users who have JavaScript<br />

disabled.<br />

Solution<br />

You can build the site to work without all the flashiness and then unobtrusively add<br />

the JavaScript functionality:<br />

<br />

<br />

<br />

<br />

<br />

Task List<br />

<br />

<br />

128 | Chapter 5: Faster, Simpler, More Fun

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

Saved successfully!

Ooh no, something went wrong!