2 ajaxcontentmws.js - Foteos Macrides (author and Copyright holder)
\r
3 Initial: June 22, 2006 - Last Revised: March 24, 2008
\r
4 Wrapper function set for getting and using the responseText and / or
\r
5 responseXML from a GET or POST XMLHttpRequest, which can be used to
\r
6 generate dynamic content for overlib or overlib2 calls, or to modify
\r
7 the content of a displayed STICKY popup dynamically.
\r
10 onmouseover="return OLgetAJAX(url, command, delay, css);"
\r
11 onmouseout="OLclearAJAX();" (if delay > 0)
\r
13 onclick="OLgetAJAX(url, command, 0, css); return false;"
\r
15 onload="OLgetAJAX(url, command, 0, css);
\r
19 is a quoted string, or unquoted string variable name or array entry, with
\r
20 the full, relative, or partial URL for a file or a server-side script (php,
\r
21 asp, or cgi, e.g. perl), and may have a query string appended (e.g.,
\r
22 'http://my.domain.com/scripts/myScript.php?foo=bar&life=grand').
\r
25 is the function reference (unquoted name without parens) of a function to
\r
26 be called when the server's response has been received (it could instead be
\r
27 an inline function, i.e., defined within the 2nd argument, or a quoted string
\r
28 for a function with parens and any args)
\r
30 delay (may be omitted unless css is included)
\r
31 is an unquoted number indicating the number of millisecs to wait before
\r
32 initiating an XMLHttpRequest GET request. It should be 0 when using onclick
\r
33 or onload, but may be a modest value such as 300 for onmouseover to avoid
\r
34 any chatter of requests. When used with onmouseover, include:
\r
35 onmouseout="OLclearAJAX();"
\r
36 to clear the request if the user does not hover for at least that long. If
\r
37 the popup is not STICKY, include an nd or nd2 call, e.g.,
\r
38 onmouseout="OLclearAJAX(); nd();"
\r
40 css (may be omitted)
\r
41 is a quoted string with the CSS class (e.g. 'ovfl510' for
\r
42 .ovfl510 {width:510px; height:145px; overflow:auto; ...} ) for a div to
\r
43 encase the responseText and set the width, height and scrollbars in the
\r
44 main text area of the popup, or the unquoted number 0 if no encasing div
\r
47 For POST substitute OLpostAJAX(url, qry, command, delay, css);
\r
50 is the string to be posted, typically a query string (without a lead ?)
\r
51 and the other arguments are as above.
\r
53 See http://www.macridesweb.com/oltest/AJAX.html for more information.
\r
56 // Initialize our global variables for this function set.
\r
57 var OLhttp=false,OLcommandAJAX=null,OLdelayidAJAX=0,OLclassAJAX='',
\r
58 OLresponseAJAX='',OLabortAJAX=0,OLdebugAJAX=0;
\r
60 // Create a series of wrapper functions (e.g. OLcmdT#() for ones which
\r
61 // use OLhttp.responseText via the OLresponseAJAX global, and OLcmdX#()
\r
62 // for ones which use OLhttp.responseXML) whose reference (unquoted name
\r
63 // without parens) is the 2nd argument in OLgetAJAX(url,command,delay,css)
\r
64 // calls. This one is for the first example in the AJAX.html support
\r
65 // document, to use the OLresponseAJAX global as the lead argument for an
\r
66 // overlib popup. Put your functions in the head, or in another imported
\r
67 // .js file, so that they will not be affected by updates of this .js file.
\r
69 function OLcmdExT1() {
\r
70 return overlib(OLresponseAJAX, TEXTPADDING,0, CAPTIONPADDING,4,
\r
71 CAPTION,'Example with AJAX content via <span '
\r
72 +'class="yellow">responseText</span>. Popup scrolls with the window.',
\r
73 WRAP, BORDER,2, STICKY, CLOSECLICK, SCROLL,
\r
75 STATUS,'Example with AJAX content via responseText of XMLHttpResponse');
\r
78 // Alert for old browsers which lack XMLHttpRequest support.
\r
79 function OLsorryAJAX() {
\r
80 alert('Sorry, AJAX is not supported by your browser.');
\r
84 // Check 2nd arg for function
\r
85 function OLchkFuncAJAX(ar){
\r
86 var t=typeof ar;return (((t=='function'))||((t=='string')&&(/.+\(.*\)/.test(ar))));
\r
89 // Alert for bad 2nd argument
\r
90 function OLnotFuncAJAX(m) {
\r
92 alert('The 2nd arg of OL'+m+'AJAX is not a function reference, nor an inline function, '
\r
93 +'nor a quoted string with a function indicated.');
\r
94 return OLclearAJAX();
\r
97 // Alert for indicating an XMLHttpRequest network error.
\r
98 function OLerrorAJAX() {
\r
99 if(OLhttp.status&&OLhttp.status!=2147746065)alert('Network error '+OLhttp.status+'. Try again later.');
\r
103 // Returns a new XMLHttpRequest object, or false for older browsers
\r
104 // which did not yet support it. Called as OLhttp=OLnewXMLHttp() via
\r
105 // the OLgetAJAX(url,command,delay,css) wrapper function.
\r
107 function OLnewXMLHttp() {
\r
109 if(window.XMLHttpRequest)eval(new Array('try{',
\r
110 'req=new XMLHttpRequest();','}catch(e){','req=f;','}').join('\n'));
\r
111 /*@cc_on @if(@_jscript_version>=5)if(!req)
\r
112 eval(new Array('try{','req=new ActiveXObject("Msxml2.XMLHTTP");',
\r
113 '}catch(e){','try{','req=new ActiveXObject("Microsoft.XMLHTTP");',
\r
114 '}catch(e){','req=f;','}}').join('\n')); @end @*/
\r
118 // Handle the OLhttp.responseText string from the XMLHttpRequest object.
\r
119 function OLdoAJAX() {
\r
120 if(OLhttp.readyState==4){
\r
121 if(OLdebugAJAX)alert(
\r
122 'OLhttp.status = '+OLhttp.status+'\n'
\r
123 +'OLhttp.statusText = '+OLhttp.statusText+'\n'
\r
124 +'OLhttp.getAllResponseHeaders() = \n'
\r
125 +OLhttp.getAllResponseHeaders()+'\n'
\r
126 +'OLhttp.getResponseHeader("Content-Type") = '
\r
127 +OLhttp.getResponseHeader("Content-Type")+'\n');
\r
128 if(OLhttp.status==200||(OLhttp.status==0&&!OLabortAJAX&&!OLie55)){
\r
129 OLresponseAJAX=OLclassAJAX?'<div class="'+OLclassAJAX+'">':'';
\r
130 OLresponseAJAX += OLhttp.responseText;
\r
131 OLresponseAJAX += OLclassAJAX?'</div>':'';
\r
132 if(OLdebugAJAX)alert('OLresponseAJAX = \n'+OLresponseAJAX);
\r
134 return (typeof OLcommandAJAX=='string')?eval(OLcommandAJAX):OLcommandAJAX();
\r
138 return OLerrorAJAX();
\r
143 // Actually make the request initiated via OLgetAJAX or OLpostAJAX, or
\r
144 // invoke a "permission denied" alert if a cross-domain URL was used.
\r
145 function OLsetAJAX(url,qry) {
\r
146 if(window.location.protocol.indexOf('http')==0&&
\r
147 (url.indexOf('file:')==0||url.indexOf('ftp:')==0)){
\r
148 alert('[object Error]\n(Cross-domain access not permitted)');return false;}
\r
149 qry=(qry||null);var s='',m=(qry)?'POST':'GET';OLabortAJAX=0;
\r
150 OLdelayidAJAX=0;eval(new Array('try{','OLhttp.open(m,url,true);',
\r
151 '}catch(e){','s=e','OLhttp=false;','}').join('\n'));if(!OLhttp){
\r
152 alert(s+'\n(Cross-domain access not permitted)');return false;}if(qry)
\r
153 OLhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
\r
154 OLhttp.onreadystatechange=OLdoAJAX;
\r
158 // Clear or abort any delayed OLsetAJAX call or pending request.
\r
159 function OLclearAJAX() {
\r
160 if(OLdelayidAJAX){clearTimeout(OLdelayidAJAX);OLdelayidAJAX=0;}
\r
161 if(OLhttp&&!OLdebugAJAX){OLabortAJAX=1;OLhttp.abort();}
\r
165 // Load a new XMLHttpRequest object into the OLhttp global, load the
\r
166 // OLcommandAJAX and OLclassAJAX globals, and initiate a GET request
\r
167 // via OLsetAJAX(url) to populate OLhttp.
\r
168 function OLgetAJAX(url,command,delay,css) {
\r
169 if(!OLchkFuncAJAX(command))return OLnotFuncAJAX('get');
\r
170 OLclearAJAX();OLhttp=OLnewXMLHttp();if(!OLhttp)return OLsorryAJAX();
\r
171 OLcommandAJAX=command;delay=(delay||0);css=(css||0);OLclassAJAX=css;
\r
172 if(delay)OLdelayidAJAX=setTimeout("OLsetAJAX('"+url+"')",delay);
\r
173 else OLsetAJAX(url);
\r
176 // Load a new XMLHttpRequest object into the OLhttp global, load the
\r
177 // OLcommandAJAX and OLclassAJAX globals, and initiate a POST request
\r
178 // via OLsetAJAX(url,qry) to populate OLhttp.
\r
179 function OLpostAJAX(url,qry,command,delay,css) {
\r
180 if(!OLchkFuncAJAX(command))return OLnotFuncAJAX('post');
\r
181 OLclearAJAX();OLhttp=OLnewXMLHttp();if(!OLhttp)return OLsorryAJAX();
\r
182 qry=(qry||0);OLcommandAJAX=command;delay=(delay||0);css=(css||0);OLclassAJAX=css;
\r
183 if(delay)OLdelayidAJAX=setTimeout("OLsetAJAX('"+url+"','"+qry+"')",delay);
\r
184 else OLsetAJAX(url,qry);
\r