Guest User

Untitled

a guest
Jul 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. function sack(file) {
  2. this.xmlhttp = null;
  3.  
  4. this.resetData = function() {
  5. this.method = "POST";
  6. this.queryStringSeparator = "?";
  7. this.argumentSeparator = "&";
  8. this.URLString = "";
  9. this.encodeURIString = true;
  10. this.execute = false;
  11. this.element = null;
  12. this.elementObj = null;
  13. this.requestFile = file;
  14. this.vars = new Object();
  15. this.responseStatus = new Array(2);
  16. };
  17.  
  18. this.resetFunctions = function() {
  19. this.onLoading = function() { };
  20. this.onLoaded = function() { };
  21. this.onInteractive = function() { };
  22. this.onCompletion = function() { };
  23. this.onError = function() { };
  24. this.onFail = function() { };
  25. };
  26.  
  27. this.reset = function() {
  28. this.resetFunctions();
  29. this.resetData();
  30. };
  31.  
  32. this.createAJAX = function() {
  33. try {
  34. this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  35. } catch (e1) {
  36. try {
  37. this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  38. } catch (e2) {
  39. this.xmlhttp = null;
  40. }
  41. }
  42.  
  43. if (! this.xmlhttp) {
  44. if (typeof XMLHttpRequest != "undefined") {
  45. this.xmlhttp = new XMLHttpRequest();
  46. } else {
  47. this.failed = true;
  48. }
  49. }
  50. };
  51.  
  52. this.setVar = function(name, value){
  53. this.vars[name] = Array(value, false);
  54. };
  55.  
  56. this.encVar = function(name, value, returnvars) {
  57. if (true == returnvars) {
  58. return Array(encodeURIComponent(name), encodeURIComponent(value));
  59. } else {
  60. this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
  61. }
  62. }
  63.  
  64. this.processURLString = function(string, encode) {
  65. encoded = encodeURIComponent(this.argumentSeparator);
  66. regexp = new RegExp(this.argumentSeparator + "|" + encoded);
  67. varArray = string.split(regexp);
  68. for (i = 0; i < varArray.length; i++){
  69. urlVars = varArray[i].split("=");
  70. if (true == encode){
  71. this.encVar(urlVars[0], urlVars[1]);
  72. } else {
  73. this.setVar(urlVars[0], urlVars[1]);
  74. }
  75. }
  76. }
  77.  
  78. this.createURLString = function(urlstring) {
  79. if (this.encodeURIString && this.URLString.length) {
  80. this.processURLString(this.URLString, true);
  81. }
  82.  
  83. if (urlstring) {
  84. if (this.URLString.length) {
  85. this.URLString += this.argumentSeparator + urlstring;
  86. } else {
  87. this.URLString = urlstring;
  88. }
  89. }
  90.  
  91. // prevents caching of URLString
  92. this.setVar("rndval", new Date().getTime());
  93.  
  94. urlstringtemp = new Array();
  95. for (key in this.vars) {
  96. if (false == this.vars[key][1] && true == this.encodeURIString) {
  97. encoded = this.encVar(key, this.vars[key][0], true);
  98. delete this.vars[key];
  99. this.vars[encoded[0]] = Array(encoded[1], true);
  100. key = encoded[0];
  101. }
  102.  
  103. urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
  104. }
  105. if (urlstring){
  106. this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
  107. } else {
  108. this.URLString += urlstringtemp.join(this.argumentSeparator);
  109. }
  110. }
  111.  
  112. this.runResponse = function() {
  113. eval(this.response);
  114. }
  115.  
  116. this.runAJAX = function(urlstring) {
  117. if (this.failed) {
  118. this.onFail();
  119. } else {
  120. this.createURLString(urlstring);
  121. if (this.element) {
  122. this.elementObj = document.getElementById(this.element);
  123. }
  124. if (this.xmlhttp) {
  125. var self = this;
  126. if (this.method == "GET") {
  127. totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
  128. this.xmlhttp.open(this.method, totalurlstring, true);
  129. } else {
  130. this.xmlhttp.open(this.method, this.requestFile, true);
  131. try {
  132. this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
  133. } catch (e) { }
  134. }
  135.  
  136. this.xmlhttp.onreadystatechange = function() {
  137. switch (self.xmlhttp.readyState) {
  138. case 1:
  139. self.onLoading();
  140. break;
  141. case 2:
  142. self.onLoaded();
  143. break;
  144. case 3:
  145. self.onInteractive();
  146. break;
  147. case 4:
  148. self.response = self.xmlhttp.responseText;
  149. self.responseXML = self.xmlhttp.responseXML;
  150. self.responseStatus[0] = self.xmlhttp.status;
  151. self.responseStatus[1] = self.xmlhttp.statusText;
  152.  
  153. if (self.execute) {
  154. self.runResponse();
  155. }
  156.  
  157. if (self.elementObj) {
  158. elemNodeName = self.elementObj.nodeName;
  159. elemNodeName = elemNodeName.toLowerCase();
  160. if (elemNodeName == "input"
  161. || elemNodeName == "select"
  162. || elemNodeName == "option"
  163. || elemNodeName == "textarea") {
  164. self.elementObj.value = self.response;
  165. } else {
  166. self.elementObj.innerHTML = self.response;
  167. }
  168. }
  169. if (self.responseStatus[0] == "200") {
  170. self.onCompletion();
  171. } else {
  172. self.onError();
  173. }
  174. /* These lines were added by Alf Magne Kalleland ref. info on the sack home page. It prevents memory leakage in IE */
  175. self.URLString = "";
  176. delete self.xmlhttp['onreadystatechange'];
  177. self.xmlhttp=null;
  178. self.responseStatus=null;
  179. self.response=null;
  180. self.responseXML=null;
  181.  
  182. break;
  183. }
  184. };
  185.  
  186. this.xmlhttp.send(this.URLString);
  187. }
  188. }
  189. };
  190.  
  191. this.reset();
  192. this.createAJAX();
Add Comment
Please, Sign In to add comment