New test.
[mono.git] / mcs / class / System.Web / resources / callback.js
1
2 function WebForm_DoCallback (id, arg, callback, ctx, errorCallback)
3 {
4         var qs = WebForm_getFormData () + "&__CALLBACKTARGET=" + id + "&&__CALLBACKARGUMENT=" + escape(arg);
5         WebForm_httpPost (document.URL, qs, function (httpPost) { WebForm_ClientCallback (httpPost, ctx, callback, errorCallback); });
6 }
7
8 function WebForm_ClientCallback (httpPost, ctx, callback, errorCallback)
9 {
10         try {
11                 var doc = httpPost.responseText;
12         } catch (e) {
13                 if (errorCallback != null)
14                         errorCallback (httpPost.responseText, ctx);
15                 return;
16         }
17         callback (doc, ctx);
18 }
19
20 function WebForm_getFormData ()
21 {
22         var qs = "";
23         var len = theForm.elements.length;
24         for (n=0; n<len; n++) {
25                 var elem = theForm.elements [n];
26                 if (qs.length > 0) qs += "&";
27                 qs += elem.name + "=" + escape (elem.value);
28         }
29         return qs;
30 }
31
32 var axName = null;
33 function WebForm_httpPost (url, data, callback)
34 {
35         var httpPost = null;
36         
37         if (typeof XMLHttpRequest != "undefined") {
38                 httpPost = new XMLHttpRequest ();
39                 httpPost.addEventListener ("load", function () { callback (httpPost);}, false );
40         } else {
41                 if (axName != null)
42                         httpPost = new ActiveXObject (axName);
43                 else {
44                         var clsnames = new Array ("MSXML", "MSXML2", "MSXML3", "Microsoft");
45                         for (n = 0; n < clsnames.length && httpPost == null; n++) {
46                                 axName = clsnames [n] + ".XMLHTTP";
47                                 try {
48                                         httpPost = new ActiveXObject (axName);
49                                 } catch (e) { axName = null; }
50                         }
51                         if (httpPost == null)
52                                 throw new Error ("XMLHTTP object could not be created.");
53                 }
54                 httpPost.onreadystatechange = function () { if (httpPost.readyState == 4) callback (httpPost); };
55         }
56         
57         httpPost.open ("POST", url, true);      // async
58         httpPost.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
59         setTimeout (function () { httpPost.send (data); }, 10);
60 }