02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

});<br />

});<br />

CHAPTER 7 ■ ENHANCING THE USER INTERFACE WITH JQUERY<br />

Save this code, then reload http://localhost/ <strong>and</strong> click a link. You should see something similar to<br />

the following appear in the console:<br />

event_id=1<br />

Creating a Modal Window<br />

The next step is to generate the HTML markup that will actually create the modal window <strong>and</strong> overlay.<br />

This markup is extremely simple, <strong>and</strong> it will basically consist of a div element wrapped around other<br />

content. For example, the New Year's Day event modal window markup will look like this:<br />

<br />

New Year's Day<br />

January 01, 2010, 12:00am—11:59pm<br />

Happy New Year!<br />

<br />

You are going to use this same modal window for other features as well (such as for displaying the<br />

editing form for events), so the actual creation of the modal window is going to be abstracted in a<br />

separate function for easy re-use. Because you will re-use more than one function, you’ll organize your<br />

script <strong>by</strong> placing all utility functions in an object literal, which is a comma-separated list of name-value<br />

pairs (for more information, see the sidebar, “Using an Object Literal for Utility Functions”).<br />

Creating the Utility Function to Check for a Modal Window<br />

At the top of init.js, declare a new object literal called fx to store your utility functions:<br />

// Makes sure the document is ready before executing scripts<br />

<strong>jQuery</strong>(function($){<br />

// Functions to manipulate the modal window<br />

var fx = {};<br />

// Pulls up events in a modal window<br />

$("li>a").live("click", function(event){<br />

// Stops the link from loading view.php<br />

event.preventDefault();<br />

// Adds an "active" class to the link<br />

$(this).addClass("active");<br />

// Gets the query string from the link href<br />

243

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

Saved successfully!

Ooh no, something went wrong!