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.

78<br />

CHAPTER 2 ■ COMMON JQUERY ACTIONS AND METHODS<br />

As an example, the following will bind a h<strong>and</strong>ler to the click event, <strong>and</strong> fire the event.<br />

$("#bar").click(function(){ console.log("Clicked!"); }).click();<br />

Using AJAX Controls<br />

The last set of <strong>jQuery</strong> methods we’re going to cover are probably the most useful, <strong>and</strong> more than likely<br />

played a large role in the widespread adoption of <strong>jQuery</strong>. The methods providing AJAX 4 functionality are<br />

incredibly useful <strong>and</strong>, especially for anyone who has built AJAX scripts in plain JavaScript before, as easy<br />

as pie.<br />

■ Note For further reading on AJAX, see the Wikipedia article here:<br />

http://en.wikipedia.org/wiki/AJAX_%programming %29<br />

For this section, you'll need an external file to access using the AJAX controls. Create a new file in the<br />

testing folder called ajax.php. Inside, insert the following code:<br />

<br />

This file will be called <strong>by</strong> the various AJAX methods available in <strong>jQuery</strong>. It will show you the data<br />

passed to the script for illustrative purposes.<br />

$.ajax()<br />

The low-level, or most basic, function for sending AJAX requests is $.ajax(). Notice that this function is<br />

called without a selector, because it doesn’t apply to the <strong>jQuery</strong> object. AJAX actions are global functions,<br />

carried out independently of the DOM.<br />

The $.ajax() function accepts one argument: an object containing settings for the AJAX call. If<br />

called without any settings, the method will load the current page <strong>and</strong> do nothing with the result.<br />

4 http://en.wikipedia.org/wiki/Ajax_%28programming%29

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

Saved successfully!

Ooh no, something went wrong!