25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

“SEEMINGLY ARBITRARY” TIMES<br />

CHAPTER 5 ■ USING THE COMMUNICATION APIS<br />

In the XMLHttpRequest Level 2 specification’s description for the readystatechange event (maintained for<br />

backward compatibility), the readyState attribute is described as changing at, get this, “some seemingly<br />

arbitrary times for historical reasons.”<br />

Browser Support for <strong>HTML</strong>5 XMLHttpRequest Level 2<br />

As shown in Table 5-3, <strong>HTML</strong>5 XMLHttpRequest is already supported in many browsers at the time of<br />

this writing.<br />

Table 5-3. Browser support for <strong>HTML</strong>5 XMLHttpRequest<br />

Browser Details<br />

Chrome Supported in version 2.0 and greater<br />

Firefox Supported in version 3.5 and greater<br />

Internet Explorer Not supported<br />

Opera Not supported<br />

Safari Supported in version 4.0 and greater<br />

Due to the varying levels of support, it is a good idea to first test if <strong>HTML</strong>5 XMLHttpRequest is<br />

supported, before you use these elements. The section “Checking for Browser Support” later in this<br />

chapter will show you how you can programmatically check for browser support.<br />

Using the XMLHttpRequest API<br />

In this section, we’ll explore the use of the XMLHttpRequest in more detail. For the sake of illustration,<br />

we’ve created a simple <strong>HTML</strong> page—crossOriginUpload.html. The sample code for the following<br />

examples is located in the code/communication folder.<br />

Checking for Browser Support<br />

Before you try to use XMLHttpRequest Level 2 functionality—such as cross-origin support—it is a good<br />

idea to check if it is supported. You can do this by checking whether the new withCredentials property is<br />

available on an XMLHttpRequest object as shown in Listing 5-7.<br />

129

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

Saved successfully!

Ooh no, something went wrong!