2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
5 var xhr = new XMLHttpRequest();
7 function onload(event) {
8 if (event.target != xhr) {
9 throw "onload event.target != xhr";
12 if (event.target.status != 200) {
13 var message = { type: "error",
14 error: event.target.status };
18 var message = { type: "load",
19 data: xhr.responseText };
24 xhr.addEventListener("load", onload, false);
25 xhr.removeEventListener("load", onload, false);
27 var message = { type: "error",
28 error: "Lost message listener!" };
32 xhr.onerror = function(event) {
33 if (event.target != xhr) {
34 throw "onerror event.target != xhr";
36 var message = { type: "error",
37 error: event.target.status };
40 xhr.onerror = xhr.onerror;
41 if (!xhr.onerror || xhr.onerror != xhr.onerror) {
42 throw "onerror wasn't set properly";
45 function onprogress(event) {
46 if (event.target != xhr) {
47 throw "onprogress event.target != xhr";
49 var message = { type: "progress",
50 current: event.loaded,
54 xhr.addEventListener("progress", onprogress, false);
56 xhr.addEventListener("foopety", function(event) {}, false);
57 xhr.removeEventListener("doopety", function(event) {}, false);
59 xhr.onloadend = function(event) {
60 var message = { type: "loadend" };
64 var upload = xhr.upload;
65 upload.onprogress = function(event) { };
66 upload.addEventListener("foo", function(event) { }, false);
67 upload.removeEventListener("foo", function(event) { }, false);
68 upload.addEventListener("load", function(event) { }, false);
69 upload.removeEventListener("foo", function(event) { }, false);
70 upload.onload = function(event) {
71 var message = { type: "upload.load" };
75 onmessage = function(event) {
76 if (xhr.DONE != 4 || XMLHttpRequest.DONE != 4) {
77 throw "xhr constants not correct!";
79 if (xhr.readystate > xhr.UNSENT) {
80 throw "XHR already running!";
82 xhr.open("POST", event.data);
83 xhr.send("Data to send");