Open-Source PHP Framework - Designed for rapid development of performance-oriented scalable applications

/webroot/js/ajax.js

[return to app]
1 /**
2  * Simplified cross-browser AJAX interface
3  * @author Eric David Benari
4  * @link http://www.Vork.us
5  */
6
var ajax = function() {}
7
8
/**
9  * Retrieve content via AJAX
10  *
11  * @param object responseFunction
12  * @param string url
13  * @param Boolean isXmlResponse Optional, default is false (string response)
14  * @return mixed Can be treated as Boolean for connection-success status
15  */
16
ajax.get = function(responseFunctionurlisXmlResponse) {
17     if (
typeof window.XMLHttpRequest != 'undefined') {
18         
ajax.req = new XMLHttpRequest();
19     } else if (
typeof window.ActiveXObject != 'undefined') { //IE5/5.5
20         
var xobjects = ['Msxml2.XMLHTTP.6.0''Msxml2.XMLHTTP.3.0''Msxml2.XMLHTTP''Microsoft.XMLHTTP'];
21         while (!
ajax.req && xobjects.length) {
22             try {
23                 
ajax.req = new ActiveXObject(xobjects.shift());
24             } catch(
e) {}
25         }
26     }
27     if (
ajax.req) {
28         try {
29             if (
navigator.appName.indexOf('Microsoft Internet Explorer') != -1) {
30                 
url += (url.indexOf('?') < '?' '&') + 'rand=' Math.random(); //IE cache fix
31             
}
32             
ajax.req.open('GET'urltrue);
33             
ajax.req.onreadystatechange = function() {
34                 if (
ajax.req.readyState == 4) {
35                     
responseFunction(typeof isXmlResponse == 'undefined' || !isXmlResponse ?
36                                      
ajax.req.responseText ajax.req.responseXML.documentElement);
37                 }
38             };
39             
ajax.req.setRequestHeader('X_REQUESTED_WITH''XMLHttpRequest');
40             
ajax.req.send(null);
41         } catch(
e) {
42             
ajax.req false;
43         }
44     }
45     return 
ajax.req;
46 }
47
48
/**
49  * Retrieve content via AJAX
50  *
51  * @param string id
52  * @param string url
53  * @return Boolean Inverted response - false on success, true on failure - so it can be used directly as a
 
clickhandler:
54  *                 <
a href="/url" onclick="ajax.load('contentbox', this.url)">...</a>
55  */
56
ajax.load = function(idurl) {
57     return (
ajax.get(function(content) {document.getElementById(id).innerHTML content;}, url) ? false true);
58 }