callback.js 1.7 KB

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