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