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

Create successful ePaper yourself

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

244<br />

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

});<br />

});<br />

var data = $(this)<br />

.attr("href")<br />

.replace(/.+?\?(.*)$/, "$1");<br />

// Logs the query string<br />

console.log( data );<br />

The first function to be stored in fx will be called initModal, <strong>and</strong> it will check whether a modal<br />

window already exists. If it does, the function will select it; otherwise, it will create a new one <strong>and</strong><br />

append it to the body tag.<br />

To see if an element already exists, use the length property after executing the <strong>jQuery</strong> function with<br />

a selector for that element. If the length property returns 0, the element does not currently exist in the<br />

document object model (DOM).<br />

Perform the check <strong>and</strong> return a modal window <strong>by</strong> inserting the following bold code into fx inside<br />

init.js:<br />

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

var fx = {<br />

};<br />

// Checks for a modal window <strong>and</strong> returns it, or<br />

// else creates a new one <strong>and</strong> returns that<br />

"initModal" : function() {<br />

// If no elements are matched, the length<br />

// property will return 0<br />

if ( $(".modal-window").length==0 )<br />

{<br />

// Creates a div, adds a class, <strong>and</strong><br />

// appends it to the body tag<br />

return $("")<br />

.addClass("modal-window")<br />

.appendTo("body");<br />

}<br />

else<br />

{<br />

// Returns the modal window if one<br />

// already exists in the DOM<br />

return $(".modal-window");<br />

}<br />

}

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

Saved successfully!

Ooh no, something went wrong!