Chat window now opens with no messages inside.
[Assignment-Trapper.git] / ajax.js
blobcab229a76f3d339fb0b1a6bc9d00af7c32a6a590
1 /*
3 Ajax Debugger.
4 Provides an in-depth tool to analyze Ajax interactions with web servers.
5 Copyright (C) 2007  Steven Ray Schronk
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 // THREADSAFE ASYNCHRONOUS XMLHTTPRequest
25 // url = url to send to server
26 // pageElement = page element to replace when data arrives
27 // waitMessage =  info to display while waiting for return from server
28 //                if waitMessage not sent, content will not "blink"
29 // callBack = javascript function name to run after data has been returned from server
30 function getPage( url, pageElement, waitMessage, callBack ) {
31                 var timer = new Date();
32                 var t_start = timer.getTime();
33                 //debugEvent(url, "get");
35   // DON'T DISTURB CONTENT UNLESS NESSISARY
36   if ( waitMessage != "" ) {  document.getElementById(pageElement).innerHTML = waitMessage; }
38 // WE USE A JAVASCRIPT FEATURE HERE CALLED "INNER FUNCTIONS"
39 // USING THESE MEANS THE LOCAL VARIABLES RETAIN THEIR VALUES AFTER THE OUTER FUNCTION
40 // HAS RETURNED. THIS IS USEFUL FOR THREAD SAFETY, SO
41 // REASSIGNING THE ONREADYSTATECHANGE FUNCTION DOESN'T STOMP OVER EARLIER REQUESTS.
42         function ajaxBindCallback(){
43                 if(ajaxRequest.readyState == 0) { window.status = "Waiting...";      }
44                 if(ajaxRequest.readyState == 1) { window.status = "Loading Page..."; }
45                 if(ajaxRequest.readyState == 2) { window.status = "Data Received...";}
46                 if(ajaxRequest.readyState == 3) { window.status = "Interactive...";  }
47                 if(ajaxRequest.readyState == 4) {
48                         window.status = "Transaction Complete...";
50       // STOP TIMER AND FIND DIFFERENCE
51       // MUST CREATE NEW TIMER INSTANCE :)
52       var timer2 = new Date();
53       var t_end = timer2.getTime();
54       var t_diff = t_end - t_start;
56       // TEST HTTP STATUS CODE - DISPLAY IN DUBUGGER AND STATUS
57       switch (ajaxRequest.status.toString()) {
58         case "200" :
59           window.status = "Page Loaded Sucessfully";
60           document.getElementById(pageElement).innerHTML = ajaxRequest.responseText;
61           debugEvent(url, "got", ajaxRequest.responseText, t_diff);
62           break;
63         case "403" :
64           window.status = "Forbidden...";
65           debugEvent(url, "error_403", ajaxRequest.responseText, t_diff);
66           break;
67         case "404" :
68           window.status = "File Not Found...";
69           debugEvent(url, "error_404", ajaxRequest.responseText, t_diff);
70           break;
71         case "500" :
72           window.status = "File Not Found...";
73           debugEvent(url, "error_500", ajaxRequest.responseText, t_diff);
74           break;
75         default :
76           window.status = "Unknown Ajax Error..."+ajaxRequest.status.toString();
77         }
78                         }
79         }
80         var ajaxRequest = null;
81         // BIND OUR CALLBACK THEN HIT THE SERVER...
82         if (window.XMLHttpRequest) {
83                 ajaxRequest = new XMLHttpRequest();
84                 ajaxRequest.onreadystatechange = ajaxBindCallback;
85                 ajaxRequest.open("GET", url, true);
86                 ajaxRequest.send(null);
87         } else if (window.ActiveXObject) {
88                 ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
89                 if (ajaxRequest) {
90                         ajaxRequest.onreadystatechange = ajaxBindCallback;
91                         ajaxRequest.open("GET", url, true);
92                         ajaxRequest.send();
93                         }
94                 }
95         if (callBack != null ) { eval(callBack+'()'); }
98 function sendData(url){
99                 // START TIMER
100                 var timer = new Date();
101                 var t_start = timer.getTime();
102 // WE USE A JAVASCRIPT FEATURE HERE CALLED "INNER FUNCTIONS"
103 // USING THESE MEANS THE LOCAL VARIABLES RETAIN THEIR VALUES AFTER THE OUTER FUNCTION
104 // HAS RETURNED. THIS IS USEFUL FOR THREAD SAFETY, SO
105 // REASSIGNING THE ONREADYSTATECHANGE FUNCTION DOESN'T STOMP OVER EARLIER REQUESTS.
106         function ajaxBindCallback(){
107                 if(ajaxRequest.readyState == 0) { window.status = "Waiting...";      }
108                 if(ajaxRequest.readyState == 1) { window.status = "Loading Page..."; }
109                 if(ajaxRequest.readyState == 2) { window.status = "Data Received...";}
110                 if(ajaxRequest.readyState == 3) { window.status = "Interactive...";  }
111                 if(ajaxRequest.readyState == 4) {
112                         window.status = "Transaction Complete...";
114       // STOP TIMER AND FIND DIFFERENCE
115       // MUST CREATE NEW TIMER INSTANCE :)
116       var timer2 = new Date();
117       var t_end = timer2.getTime();
118       var t_diff = t_end - t_start;
120       // TEST HTTP STATUS CODE - DISPLAY IN DUBUGGER AND STATUS
121       switch (ajaxRequest.status.toString()) {
122         case "200" :
123           debugEvent(url, "got", ajaxRequest.responseText, t_diff);
124           break;
125         case "403" :
126           debugEvent(url, "error_403", ajaxRequest.responseText, t_diff);
127           break;
128         case "404" :
129           debugEvent(url, "error_404", ajaxRequest.responseText, t_diff);
130           break;
131         case "500" :
132           debugEvent(url, "error_500", ajaxRequest.responseText, t_diff);
133           break;
134         }
135                         }
136         }
137         var ajaxRequest = null;
138         // BIND OUR CALLBACK THEN HIT THE SERVER...
139         if (window.XMLHttpRequest) {
140                 ajaxRequest = new XMLHttpRequest();
141                 ajaxRequest.onreadystatechange = ajaxBindCallback;
142                 ajaxRequest.open("GET", url, true);
143                 ajaxRequest.send(null);
144         } else if (window.ActiveXObject) {
145                 ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
146                 if (ajaxRequest) {
147                         ajaxRequest.onreadystatechange = ajaxBindCallback;
148                         ajaxRequest.open("GET", url, true);
149                         ajaxRequest.send();
150                         }
151                 }