04.11.2015 Views

javascript

Create successful ePaper yourself

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

Chapter 12: Events<br />

Besides the usual properties, the event object for pageshow includes a property called persisted .<br />

This is a Boolean value that is set to true if the page is stored in the bfcache or false if the page i s not.<br />

The property can be checked in the event handler as follows:<br />

(function(){<br />

var showCount = 0;<br />

EventUtil.addHandler(window, “load”, function(){<br />

alert(“Load fired”);<br />

});<br />

EventUtil.addHandler(window, “pageshow”, function(){<br />

showCount++;<br />

alert(“Show has been fired “ + showCount +<br />

“ times. Persisted? “ + event.persisted);<br />

});<br />

})();<br />

The persisted property lets you determine if a different action must be taken depending on the state<br />

of the page in the bfcache.<br />

The pagehide event is a companion to pageshow and fires whenever a page is unloaded from the<br />

browser, firing immediately before the unload event. As with the pageshow event, pagehide fires<br />

on the document even though the event handler must be attached to the window . The event object also<br />

includes the persisted property, though there is a slight difference in its usage. Consider the following:<br />

EventUtil.addHandler(window, “pagehide”, function(event){<br />

alert(“Hiding. Persisted? “ + event.persisted);<br />

});<br />

You may decide to take a different action based on the value of persisted when pagehide fires. For the<br />

pageshow event, persisted is set to true if the page has been loaded from the bfcache; for the pagehide<br />

event, persisted is set to true if the page will be stored in the bfcache once unloaded. So the first time<br />

pageshow is fired, persisted is always false, whereas while the first time pagehide is fired, persisted<br />

will be true (unless the page won ’ t be stored in the bfcache).<br />

Pages that have an onunload event handler assigned are automatically excluded<br />

from the bfcache, even if the event handler is empty. The reasoning is that onunload<br />

is typically used to undo what was done using onload, and so skipping onload the<br />

next time the page is displayed could cause it to break.<br />

Mobile Safari Events<br />

Safari for the iPhone and iPod Touch has several proprietary events designed to inform developers when<br />

specific events occurs. Since both the iPhone and iPod Touch are mouseless and keyboardless, the<br />

regular mouse and keyboard events simply aren ’ t enough to create a completely interactive web page<br />

designed with mobile Safari in mind. The following events work only on mobile Safari for the iPhone<br />

and iPod Touch.<br />

417

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

Saved successfully!

Ooh no, something went wrong!