Demo - Firefox 3.5: XMLHttpRequest improvements
By Paul Rouget on Wednesday 15 April 2009, 14:00 - shared - Permalink
The implementation of XMLHttpRequest in Firefox 3.1/3.5 has been improved.
As a web developer, you should care, among other things, about two new features:
Monitoring progress
XHR now provides a new event: progress. That means you can track the progress of your request (like the percentage of a download). It's easy to use:
var req = new XMLHttpRequest();
req.addEventListener("progress", updateProgress, false);
req.open();
// progress on transfers from the server to the client (downloads)
function updateProgress(evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
// ...
} else {
// Unable to compute progress information
// since the total size is unknown
}
}
Cross-site HTTP requests
This is a really new cool feature. You can now do a request from you web page to a different domain. But the remote content targeted should provide an Access-Control (through a HTTP header - .htaccess, PHP, ... - or a XML header). See MDC for more informations: https://developer.mozilla.org/En/HT....
Example of a .htaccess:
<FilesMatch "foobar.xml"> Header set Access-Control-Allow-Origin "http://www.mozbox.org" </FilesMatch>
The demo

This demo includes these two new features. A XHR is performed from my website (www.mozbox.org) to the Mozilla website (people.mozilla.com). The XHR downloads a big file (5.4Mo).
- the progress of the download is displayed via a canvas progress bar (a <canvas/> progressbar: http://www.netzgesta.de/gauge/)
- the request is allowed because I add a header via a .htacces: Header set Access-Control-Allow-Origin "http://www.mozbox.org"
Try it :)
Comments
Nitpicking: it'd be even more awesome if this validated... it's currently a little confused about whether it's xhtml or html 4 (it would validate as xhtml 1.0 transitional if served with the right mimetype, doctype and xmlns="http://www.w3.org/1999/xhtml", it does not validate as html 4 because of the "short" close tags).
it does not validate as html 4 because of the "short" close tags).
These are two great new features. I'm sure they will be widely used! Thanks for the demo.
These are great features, but I'm encountering a major problem with the new "Cross Domain" XHR feature in Firefox 3.5 - I can't find any way to do Javascript "feature detection" on it!
I'd like to try to detect whether the browser supports the Accepts header and gracefully fall back to using a SWF file proxy as a fallback. I'd rather avoid using version numbers. The new behavior is so seamless I can't find any way to detect it :(
Hey
I'm also looking for a way to detect by advance if a browser supports the XHR onProgress event
That would allow to get rid of the SWF loading for those browsers
Any news on that ?
ok, I found the way to detect the support of this feature
function checkSupport() {
var oEl = new XMLHttpRequest();
if(!oEl) // if standard XHR does not exist, we're probably on IE6 that do not support event progress anyway
return false;
return ('onprogress' in oEl);
}
I used it to display the loading state of images (post in french) :
http://jpv.typepad.com/blog/2009/11...