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.

crossOriginRequest.upload.onprogress = function(e) {<br />

var total = e.total;<br />

var loaded = e.loaded;<br />

}<br />

if (e.lengthComputable) {<br />

// do something with the progress information<br />

}<br />

Building an Application Using XMLHttpRequest<br />

CHAPTER 5 ■ USING THE COMMUNICATION APIS<br />

In this example, we’ll look at uploading race geolocation coordinates to a web server hosted on a<br />

different origin. We use the new progress events to monitor the status of the HTTP request including the<br />

upload percentage. Figure 5-5 shows the application in action.<br />

Figure 5-5. A web application that uploads geolocation data<br />

For the sake of illustration, we’ve created the <strong>HTML</strong> file crossOrignUpload.html. The following steps<br />

highlight the important parts of building the cross-origin upload page shown in Figure 5-5. The sample<br />

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

First, create a new XMLHttpRequest object, as shown in the following example.<br />

var xhr = new XMLHttpRequest();<br />

Next, do check if cross-origin XMLHttpRequest is supported in the browser, as shown in the<br />

following example.<br />

131

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

Saved successfully!

Ooh no, something went wrong!