1 /* Simple AJAX Code-Kit (SACK) */
2 /* ©2005 Gregory Wild-Smith */
3 /* www.twilightuniverse.com */
4 /* Software licenced under a modified X11 licence, see documentation or authors website for more details */
7 this.AjaxFailedAlert = "Your browser does not support the enhanced functionality of this website, and therefore you will have an experience that differs from the intended one.\n";
8 this.requestFile = file;
11 this.encodeURIString = true;
14 this.onLoading = function() { };
15 this.onLoaded = function() { };
16 this.onInteractive = function() { };
17 this.onCompletion = function() { };
19 this.createAJAX = function() {
21 this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
24 this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
29 if(!this.xmlhttp && typeof XMLHttpRequest != "undefined")
30 this.xmlhttp = new XMLHttpRequest();
36 this.setVar = function(name, value){
37 if (this.URLString.length < 3){
38 this.URLString = name + "=" + value;
40 this.URLString += "&" + name + "=" + value;
44 this.encVar = function(name, value){
45 var varString = encodeURIComponent(name) + "=" + encodeURIComponent(value);
49 this.encodeURLString = function(string){
50 varArray = string.split('&');
51 for (i = 0; i < varArray.length; i++){
52 urlVars = varArray[i].split('=');
53 if (urlVars[0].indexOf('amp;') != -1){
54 urlVars[0] = urlVars[0].substring(4);
56 varArray[i] = this.encVar(urlVars[0],urlVars[1]);
58 return varArray.join('&');
61 this.runResponse = function(){
65 this.runAJAX = function(urlstring){
66 this.responseStatus = new Array(2);
67 if(this.failed && this.AjaxFailedAlert){
68 alert(this.AjaxFailedAlert);
71 if (this.URLString.length){
72 this.URLString = this.URLString + "&" + urlstring;
74 this.URLString = urlstring;
77 if (this.encodeURIString){
78 var timeval = new Date().getTime();
79 this.URLString = this.encodeURLString(this.URLString);
80 this.setVar("rndval", timeval);
82 if (this.element) { this.elementObj = document.getElementById(this.element); }
85 if (this.method == "GET") {
86 var totalurlstring = this.requestFile + "?" + this.URLString;
87 this.xmlhttp.open(this.method, totalurlstring, true);
89 this.xmlhttp.open(this.method, this.requestFile, true);
91 if (this.method == "POST"){
93 this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
97 this.xmlhttp.send(this.URLString);
98 this.xmlhttp.onreadystatechange = function() {
99 switch (self.xmlhttp.readyState){
107 self.onInteractive();
110 self.response = self.xmlhttp.responseText;
111 self.responseXML = self.xmlhttp.responseXML;
112 self.responseStatus[0] = self.xmlhttp.status;
113 self.responseStatus[1] = self.xmlhttp.statusText;
115 if(self.execute){ self.runResponse(); }
116 if (self.elementObj) {
117 var elemNodeName = self.elementObj.nodeName;
118 elemNodeName.toLowerCase();
119 if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea"){
120 self.elementObj.value = self.response;
122 self.elementObj.innerHTML = self.response;