04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 21: Upcoming API s<br />

The navigator.onLine property is supported in IE 4 and later, Firefox 2 and later, and Opera 9.5 and<br />

later. Firefox updates this property when the user manually tells the browser to go into offline mode<br />

or when network connectivity is lost (Windows and Linux only). IE versions 4 through 7 change<br />

navigator.onLine only when the user manually changes the browser to or from offline mode. IE 8<br />

also updates the property when network connectivity is lost or restored.<br />

IE 8, Firefox 3, and Opera 9.5 introduced support for the offline and online events in connection to<br />

the navigator.onLine property changing.<br />

Changes to History<br />

One of the big challenges associated with Ajax applications is the lost functionality of the browser ’ s Back<br />

button. Numerous hacks and workarounds have been used to reenable the familiar Back button<br />

functionality that many users are accustomed to. HTML 5 seeks to make this easier by augmenting the<br />

BOM history object.<br />

In an Ajax application, the page may never reload or navigate away to another page, which means that<br />

it ’ s the state of the application that changes rather than the location. HTML 5 adds the pushState()<br />

method to history , allowing you to add state information to the history stack. This method accepts<br />

three arguments: an object containing state information, the title of the state, and an optional URL<br />

indicating how the browser ’ s URL should change to reflect the state. When pushState() is called, the<br />

state information is added to the history stack as if the user had navigated to a new page.<br />

The history stack in HTML 5 can contain a mix of URL and state information. When the Back or Forward<br />

button is clicked, the history stack is popped to determine what to do next. If the next entry in the<br />

history stack is a URL, then the browser navigates to that URL; if the entry is state information, then<br />

a popstate event is fired on the document ’ s body and bubbles up to window . The event object for<br />

popstate has a state property that contains the object that was passed into pushState() . Here is an<br />

example:<br />

//add some state to the history stack<br />

history.pushState({ mode: “edit”}, “Editing”);<br />

//listen for a change in state<br />

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

});<br />

var state = event.state;<br />

if (mode == “edit”){<br />

//do something<br />

}<br />

At the time of this writing, no browsers have implemented the changes to history. There is still some<br />

question as to how the second argument of pushState() will actually be used, though user agents will<br />

likely use it as the title of the document when the state changes.<br />

693

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

Saved successfully!

Ooh no, something went wrong!