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.

Create a JavaScript Initialization File<br />

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

Your app is following progressive enhancement guidelines, so all scripts will be housed in an external file<br />

called init.js. It will reside in the public js folder (/public/assets/js/init.js), <strong>and</strong> it will contain all of<br />

the custom <strong>jQuery</strong> code for your app.<br />

Include the Initialization File in the Application<br />

Before any of your scripts will be available to your app, you will need to include the initialization file in<br />

the application. Your app will use <strong>jQuery</strong> syntax, so the initialization file needs to be included after the<br />

script that loads the <strong>jQuery</strong> library in footer.inc.php.<br />

You include the file in your app <strong>by</strong> inserting the following bold code into footer.inc.php:<br />

<br />

<br />

google.load("jquery", "1");<br />

<br />

<br />

<br />

<br />

Ensuring the Document Is Ready Before Script Execution<br />

After creating init.js, use the document.ready shortcut from <strong>jQuery</strong> to ensure that no scripts execute<br />

before the document is actually ready to be manipulated. Insert the following code into init.js:<br />

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

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

// A quick check to make sure the script loaded properly<br />

console.log("init.js was loaded successfully.");<br />

});<br />

Save this file <strong>and</strong> load http://localhost/ in your browser with the Firebug console open. After the<br />

file is loaded, you should see the following result appear in the console:<br />

init.js was loaded successfully.<br />

Creating a New Stylesheet for Elements Created <strong>by</strong> <strong>jQuery</strong><br />

To ensure the elements created with <strong>jQuery</strong> look right when you start building them, you’re going to<br />

jump a bit ahead here <strong>and</strong> create a new CSS file to store styling information for the elements you’ll create<br />

with the <strong>jQuery</strong> scripts you’re about to write.<br />

237

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

Saved successfully!

Ooh no, something went wrong!