![]()
Snippets of A |
|
|
What are snippets? |
|
prototype and with no additional subroutines to help build the Ajax instance, and not even echo handlers as additional globally defined functions.
function Ajax(echoFunction, responseType){//version 1.1.0 this.request=null; this.response=null; this.callingMethod=''; this.responseType=(typeof(responseType)=='string' && responseType.toLowerCase()=='responsexml ')? 'responseXML':'responseText'; this.echoFunction=echoFunction||null; /********* M E T H O D *********/ this.initialize=function(){ this.response=null; this.callingMethod=''; if(!this.request){ if(window['(...)ttpRequest']){/*IE7, Mozillas*/ try{this.request=new (...)ttpRequest();}catch(e){this.request=null;}; } else if(window['ActiveXObject']){/*IE<IE7*/ var ajaxMSversions=[ /*'Ms(...).DOMDocument.5.0', 'Ms(...).DOMDocument.4.0', 'Ms(...).DOMDocument.3.0', 'MS(...).DOMDocument',*/ 'Ms(...).(...)TTP', 'Microsoft.(...)TTP' ]; for(var v=0; v<ajaxMSversions.length; v++){ try{this.request=new ActiveXObject(ajaxMSversions[v]); return this.request;}catch(e){this.request=null;}; } } else if(window['createRequest']){ try{this.request=window.createRequest();}catch(e){this.request=null;}; } else{alert(X M L not enabled. Impossible to proceed.');} }; return this.request; } /********* M E T H O D *********/ this.get=this.send=function(address, query, echoFunction, responseType){ if(!address || !this.initialize()){return false;}; this.callingMethod='GET'; this.responseType=responseType||this.responseType; query=query||''; query=query.replace(/\?/, ''); this.request.open('GET', (address+'?'+query), true); this.request.setRequestHeader('Content-Type', 'text/xml'); if(typeof(echoFunction)!="function"){this.request.onreadystatechange=this.echo(this);/*currying*/} else{this.request.onreadystatechange=echoFunction;}; this.request.send(null); } /********* M E T H O D *********/ this.post=function(address, send, echoFunction, responseType){ if(!address || !this.initialize()){return false;}; this.callingMethod='POST'; this.responseType=responseType||this.responseType; send=send||''; this.request.open('POST', address, true); this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); if(typeof(echoFunction)!="function"){this.request.onreadystatechange=this.echo(this);/*currying*/} else{this.request.onreadystatechange=echoFunction;}; this.request.send(send); } /********* M E T H O D *********/ this.head=function(address, send, echoFunction){ if(!address || !this.initialize()){return false;}; this.callingMethod='HEAD'; send=send||''; this.request.open('HEAD', address, true); this.request.setRequestHeader('Content-Type', 'text/xml'); if(typeof(echoFunction)!="function"){this.request.onreadystatechange=this.echo(this);/*currying*/} else{this.request.onreadystatechange=echoFunction;}; this.request.send((send||null)); } /********* M E T H O D *********/ this.error=function(statusError){ if(statusError){ this.response=(this.request && this.request.status)? 'Ajax Error: '+this.request.status+': '+this.request.statusText: 'Ajax Error: Requested document may be temporarily unavailable'; alert(this.response); return this.response; } else{return false;}; } /********* M E T H O D *********/ this.echo=function(ajaxInstance){ return function(){//currying if(ajaxInstance.request.readyState==4 || ajaxInstance.request.readyState=='complete'){ if(ajaxInstance.request.status==200 || ajaxInstance.request.status==100){ ajaxInstance.response= (ajaxInstance.callingMethod=='GET' || ajaxInstance.callingMethod=='POST')?ajaxInstance.request[ajaxInstance.responseType]: (ajaxInstance.callingMethod=='HEAD')?ajaxInstance.request.getAllResponseHeaders():false; if(typeof(ajaxInstance.echoFunction)=="function"){ return ajaxInstance.echoFunction(ajaxInstance.response); };/*no function passed as parameter: a default behaviour:*/ return ajaxInstance.response;/*response is STORED in the instance.response property, ready for further manipulation*/ } else{return ajaxInstance.error(1);}; } else{return ajaxInstance.error(0);}; }//currying over } /*class ends - Keep this comment to reuse freely: http://www.fullposter.com/?1/ */}Remove colors
//passing as parameter your echo handling function: var myajax=new Ajax( function(response){alert(response);} ); //now run ajax for instance with post (placeholder values passed): myajax.post('myphp.php', 'id=1&foo=2&bar=3'); //or you could also do: var myajax=new Ajax(); myayax.echoFunction=function(response){alert(response);} //now run ajax for instance with post (placeholder values passed: myajax.post('myphp.php', 'id=1&foo=2&bar=3'); /*echoFunction is a class property, and is case sensitive: it can host a function defined by you*/Remove colors
var myajax=new Ajax(); and regardless, for simplicity of explanation, of how you specified the response handler:myajax.get:
myajax.post:
get. You'd mostly use post rather than get, also for several security concerns that we can't elaborate here like, for instance, also (Cross Site Scripting * ) attacks that would be greatly favoured by using Ajax get methods (and yet not defused by merely recurring to the use of post).myajax.head:
get, but it accepts only the first 2 arguments described there.ajaxInstance.request.status==200 || ajaxInstance.request.status==100Remove colorsNormally only the request status 200 is considered in Ajax applications, yet taking into account at least also an Http "error" 100 code accomodates nicely some Macintosh specific browsers that at times may not detect correctly what is actually a 200 code. As a matter of fact an http response code 200 flags success (so called
OK), but also a response 100 does: Error 100 says The request was successful. The process can now continue so why at least this is not considered into standard Ajax applications is not clear to me (for more Http error codes: Http Error codes). I was even tempted to use: ajaxInstance.request.status<400.
& + % if not even question marks, these chars are treated like url components. The most dreaded consequence of this, fit to jeopardize your whole ajax application, is that any & is misunderstood like one more query item separator.
encodeURIComponent: if the your database is already set to UTF-8 (recommended), no further action is required; otherwise the server side script, if PHP, has to decode the encoded part with utf8_decode, and it cannot be excluded that before printing again to the client, if this is the case, you may also have to use utf8_encode.
send=send.replace(/\&/g, '#38#');
send=send.replace(/%/g, '#37#');
send=send.replace(/\+/g, '#43#');
& + % from all the Ajax replacements.
encodeURIComponent on the text sent to ajax. Unfortunately this caveat was not always well documented in the first ajax tutorials we saw online years ago.