public
Last active

An external resource request function for modern (i.e. BlobBuilder-supporting) browsers and FF5.

  • Download Gist
README.md
Markdown

API

XMLHttpRequest request({
    String url
    // type can be buffer, blob, text, or document
    optional String type = "buffer"
    optional String|ArrayBuffer|Blob|FormData|Document data
    optional Function callback(type data)
    optional Function onerror()
})

Note: In order to use the document type with text/html requests, make sure to include my DOMParser HTML extension.

request.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
var request = (function(view) {
"use strict";
var
XHR = XMLHttpRequest
, BB = view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder
, DOMURL = view.URL || view.webkitURL || view
, dom_parser = new DOMParser
, resp_type_supported = "responseType" in new XHR
, buff_supported
, blob_supported
, text_supported
, doc_supported
, request = function(opts) {
var
url = opts.url
, data = "data" in opts ? opts.data : null
// type can be buffer, blob, text, or document
// note that the blob type is unsupported in FF5 without BlobBuilder.js
, type = opts.type || "buffer"
, blob_req = type === "blob"
, buff_req = type === "buffer"
, binary_req = buff_req || blob_req
, doc_req = type === "document"
, text_req = type === "text"
, resp_type = type
, callback = opts.callback
, onerror = opts.onerror
, req = new XHR
;
req.open(data === null ? "GET" : "POST", url, true);
if (buff_req || blob_req && !blob_supported) {
resp_type = "arraybuffer"
}
if (doc_req && !doc_supported) {
resp_type = "";
}
req.responseType = resp_type;
if (callback) {
req.addEventListener("load", function() {
var
type = req.getResponseHeader("Content-Type")
, data = req.response
, text, bb
;
if (binary_req) {
data = data || req.mozResponseArrayBuffer;
if (blob_req && !blob_supported) {
bb = new BB;
bb.append(data);
data = bb.getBlob(type);
}
} else if (text_req) {
data = data || req.responseText;
} else if (doc_req) {
if (!doc_supported) {
data =
req.responseXML
|| dom_parser.parseFromString(req.responseText, type)
;
}
}
callback.call(req, data);
}, false);
}
if (onerror) {
req.addEventListener("error", function() {
onerror.apply(req, arguments);
}, false);
}
req.send(data);
return req;
}
, test_object_url
, test_resp_type = function(type) {
var test_req = new XHR;
test_req.open("GET", test_object_url, false);
test_req.responseType = type;
test_req.send();
return test_req.response !== null;
}
;
if (resp_type_supported && BB) {
test_object_url = DOMURL.createObjectURL((new BB).getBlob("text/html"));
buff_supported = test_resp_type("arraybuffer")
blob_supported = test_resp_type("blob");
text_supported = test_resp_type("text")
doc_supported = test_resp_type("document");
DOMURL.revokeObjectURL(test_object_url);
}
return request;
}(self));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.