W3C home > Mailing lists > Public > public-webapps@w3.org > July to September 2009

Re: HTTP status code equivalents for file:// operations - compat with xhr

From: Michael A. Puls II <shadow2531@gmail.com>
Date: Wed, 19 Aug 2009 23:50:18 -0400
To: "Jonas Sicking" <jonas@sicking.cc>
Cc: arun@mozilla.com, public-webapps@w3.org
Message-ID: <op.uyxzl3qg1ejg13@sandra-svwliu01>
On Wed, 19 Aug 2009 15:18:03 -0400, Michael A. Puls II  
<shadow2531@gmail.com> wrote:

> However note that I'm not sure that failing to parse should fire an
> error event. For someone only caring about responseText things loaded
> just fine. (I think I actually changed firefox from what you describe
> to what I describe sometime after Firefox 2, for this very reason).
> The "XML" part of the name "XMLHttpRequest" was never very true.
>  Ah, understood. I think I could work around that by checking  
> responseXML to see if it's right. If it's not, perhaps I could manually  
> dispatch an 'error', if there's a way to manually, create and init a  
> progress event so that you can dispatch it. Will play with that.

I can do this in FF:

function loadXMLFile(fileURI, load_callback, error_callback) {
     var req = new XMLHttpRequest();
     req.addEventListener("load", function(e) {
         var doc = e.target.responseXML;
         if (doc instanceof XMLDocument && doc.documentElement instanceof  
Element &&  
"parsererror").length === 0) {
         } else {
             var evt = document.createEvent("ProgressEvent");
             evt.initProgressEvent("error", false, false,  
e.lengthComputable, e.loaded, e.total);
             evt.message = "XML Parse Errror";
     }, false);
     req.addEventListener("error", function(e) {
         if (typeof e.message !== "string") {
             e.message = "Unknown error - Perhaps cross-origin violation";
     }, false);
     try {
         req.open("GET", fileURI);
     } catch (err) {
         var evt = document.createEvent("ProgressEvent");
         evt.initProgressEvent("error", false, false, 0, 0, 0);
         evt.message = "Some kind of Access error - Could be File Not  
Found, Access Denied or something else";

// Usage:
loadXMLFile("test.xml", function(e) {
}, function(e) {

Then, XHR2 would be wrapped again for a loadTextFile().

Anyway, that at least makes the load and error callbacks fire only/exactly  
when they should and still passes in a ProgressEvent, but with a .message  
getter with some error info. Still not possible to get the error info  
specific enough for each case though.

With _lookupGetter__ and __defineGetter__, I might be able to make .status  
return 200, 404 and 403 for some of the conditions if I wanted.

In the try{} part, I could create an anchor element, set its href to  
fileURI so that it's resolved and compare it to the document's location to  
make sure the requested file is in the document's directory or one of its  
subdirectories. That way, I could at least detect that particular security  
violation and fire 'error' for it.

But, all of this is just trying to force what XHR and ProgressEvents were  
not even closely designed to do. Wrapped like that, it is at least a  
little bit closer to DOM3 L&S.

Received on Thursday, 20 August 2009 03:51:01 UTC

This archive was generated by hypermail 2.3.1 : Friday, 27 October 2017 07:26:18 UTC