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 />

"duration":6000,<br />

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

if(count++==200)<br />

{<br />

$(this).stop(true, true);<br />

}<br />

}<br />

});<br />

H<strong>and</strong>ling Events<br />

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

In many scripts, it’s desirable to have certain actions occur when certain events, or browser actions,<br />

occur. Support is built into <strong>jQuery</strong> to h<strong>and</strong>le browser events, which you’ll learn in this section.<br />

Browser Events<br />

Browser events occur when the browser itself experiences a change or error.<br />

.error()<br />

If a browser error occurs, this event is triggered. One common instance of a browser error would be an<br />

image tag that tries to load an image that does not exist. The .error() method allows developers to bind<br />

a h<strong>and</strong>ler (i.e., a function to be fired if the event occurs) to the event.<br />

Create an image tag that tries to display an image that doesn’t exist, <strong>and</strong> attach an error h<strong>and</strong>ler to<br />

the error event that outputs a message to the console:<br />

$("", {<br />

"src":"not/an/image.png",<br />

"alt":"This image does not exist"<br />

})<br />

.error(function(){<br />

console.log("The image cannot be loaded!");<br />

})<br />

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

Upon execution of this code, the console will display the following:<br />

71

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

Saved successfully!

Ooh no, something went wrong!