Managment page has better look and feel.
[Assignment-Trapper.git] / ajax.js
blob2c4816123483b2e18c71db95a84bb27a19ba21b8
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 ) {
32                 // START TIMER
33                 var timer = new Date();
34                 var t_start = timer.getTime();
35                 //debugEvent(url, "get");
37   // DON'T DISTURB CONTENT UNLESS NESSISARY
38   if ( waitMessage != "" ) {  document.getElementById(pageElement).innerHTML = waitMessage; }
40 // WE USE A JAVASCRIPT FEATURE HERE CALLED "INNER FUNCTIONS"
41 // USING THESE MEANS THE LOCAL VARIABLES RETAIN THEIR VALUES AFTER THE OUTER FUNCTION
42 // HAS RETURNED. THIS IS USEFUL FOR THREAD SAFETY, SO
43 // REASSIGNING THE ONREADYSTATECHANGE FUNCTION DOESN'T STOMP OVER EARLIER REQUESTS.
44         function ajaxBindCallback(){
45                 if(ajaxRequest.readyState == 0) { window.status = "Waiting...";      }
46                 if(ajaxRequest.readyState == 1) { window.status = "Loading Page..."; }
47                 if(ajaxRequest.readyState == 2) { window.status = "Data Received...";}
48                 if(ajaxRequest.readyState == 3) { window.status = "Interactive...";  }
49                 if(ajaxRequest.readyState == 4) {
50                         window.status = "Transaction Complete...";
52       // STOP TIMER AND FIND DIFFERENCE
53       // MUST CREATE NEW TIMER INSTANCE :)
54       var timer2 = new Date();
55       var t_end = timer2.getTime();
56       var t_diff = t_end - t_start;
58       // TEST HTTP STATUS CODE - DISPLAY IN DUBUGGER AND STATUS
59       switch (ajaxRequest.status.toString()) {
60         case "200" :
61           window.status = "Page Loaded Sucessfully";
62           document.getElementById(pageElement).innerHTML = ajaxRequest.responseText;
63           debugEvent(url, "got", ajaxRequest.responseText, t_diff);
64           break;
65         case "403" :
66           window.status = "Forbidden...";
67           debugEvent(url, "error_403", ajaxRequest.responseText, t_diff);
68           break;
69         case "404" :
70           window.status = "File Not Found...";
71           debugEvent(url, "error_404", ajaxRequest.responseText, t_diff);
72           break;
73         case "500" :
74           window.status = "File Not Found...";
75           debugEvent(url, "error_500", ajaxRequest.responseText, t_diff);
76           break;
77         default :
78           window.status = "Unknown Ajax Error..."+ajaxRequest.status.toString();
79         }
80                         }
81         }
82         var ajaxRequest = null;
83         // BIND OUR CALLBACK THEN HIT THE SERVER...
84         if (window.XMLHttpRequest) {
85                 ajaxRequest = new XMLHttpRequest();
86                 ajaxRequest.onreadystatechange = ajaxBindCallback;
87                 ajaxRequest.open("GET", url, true);
88                 ajaxRequest.send(null);
89         } else if (window.ActiveXObject) {
90                 ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
91                 if (ajaxRequest) {
92                         ajaxRequest.onreadystatechange = ajaxBindCallback;
93                         ajaxRequest.open("GET", url, true);
94                         ajaxRequest.send();
95                         }
96                 }
97         if (callBack != null ) { eval(callBack+'()'); }