Требование: PHP => 5.3, часть 2
[cswow.git] / js / ajax.js
blob63aa1b38b75d88b528dd67d0b80e6efb7ceee1c1
1 //\r
2 // Cross browser ajax upload class\r
3 //\r
4 var my_AJAX = new function (){\r
5         var cache = new Array();\r
6         //\r
7         // Cross browser ajax object creation\r
8         //\r
9         this.getXmlHttp = function (){\r
10                 var xmlhttp = null;\r
11                 if (typeof XMLHttpRequest != "undefined")\r
12                         xmlhttp = new XMLHttpRequest();\r
13                 if (xmlhttp) return xmlhttp;\r
14                 try {\r
15                         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");\r
16                 } catch (e1) {\r
17                         try {\r
18                                 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");\r
19                         } catch (e2) {}\r
20                 }\r
21                 return xmlhttp;\r
22         }\r
23         //\r
24         // Upload data\r
25         //\r
26         this.upload = function(mode, url, oncomplete) {\r
27                 if (this.isCached(url)){\r
28                         oncomplete(this.getCache(url));\r
29                         return;\r
30                 }\r
31                 var ajax = this.getXmlHttp();\r
32                 if (!ajax) return 0;\r
33                 // Open url\r
34                 if (mode == "GET") {\r
35                         ajax.open(mode, url);\r
36                 } else {\r
37                         ajax.open(mode, url);\r
38                         ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")\r
39                 }\r
40                 // Progress handler\r
41                 var self = this;\r
42                 ajax.onreadystatechange = function() {\r
43                 switch (ajax.readyState) {\r
44                         case 1: break;\r
45                         case 2: break;\r
46                         case 3: break;\r
47                         case 4:\r
48                                 self.addCache(ajax.responseText, url);\r
49                                 ajax.onreadystatechange = function() {};\r
50                                 oncomplete(ajax.responseText);\r
51                                 break;\r
52                         }\r
53                 };\r
54                 // Begin\r
55                 ajax.send(null);\r
56         };\r
57         this.GETupload = function(url, oncomplete) {\r
58                 this.upload('GET', url, oncomplete);\r
59         };\r
60         this.POSTupload = function(url, oncomplete) {\r
61                 this.upload('POST', url, oncomplete);\r
62         };\r
63         //\r
64         // Cache functions\r
65         //\r
66         this.addCache = function(text, key) {\r
67                 cache[key] = text;\r
68         };\r
69         this.getCache = function(key) {\r
70                 return cache[key];\r
71         };\r
72         this.isCached = function(url) {\r
73                 return cache[url] ? true : false;\r
74         };\r
75 }\r
77 //\r
78 // HTML added in innerHTML can contain java script code\r
79 // Need upload it and use\r
80 var scr_includes = new Array();\r
81 function execHTMLScripts(node)\r
82 {\r
83         parseScript(node.getElementsByTagName('script'), 0);\r
84 }\r
85 function parseScript(list, n)\r
86 {\r
87         if (list.length<=n)\r
88                 return;\r
89         var script = list[n++];\r
90         var id = script.src ? script.src : script.id;\r
91         if (id && !scr_includes[id])\r
92         {\r
93                 scr_includes[id] = true;\r
94                 var s = document.createElement('script');\r
95                 document.getElementsByTagName('head')[0].appendChild(s);\r
96                 s.id  = script.id;\r
97                 s.text= script.text;\r
98                 // in IE onload event not work, but work onreadystatechange\r
99                 if (script.src){\r
100                         s.onload                         = function(){this.onload = this.onreadystatechange = function(){}; parseScript(list, n);}\r
101                         s.onreadystatechange = function(){if (this.readyState == 'loaded' || this.readyState == 'complete') this.onload();}\r
102                         s.src = script.src;\r
103                         return;\r
104                 }\r
105         }\r
106         else if (!id)\r
107                 eval(script.text);\r
108         parseScript(list, n);\r