Hi aosmith,
your code looks fine to me and maybe what it needs is a little adjustment.
Please don't get confused by the lines that i have implemented on this demo, my intention is to provide better support in AJAX handling, no matter what version of browsers that may come to handle it. You can just concentrate the whole thing that is inside the request handler function and forget about the rest.
Here's the code for the demo:
<script type="text/javascript">
// <![CDATA[
var ie = (( !!document.all && !!!document.getElementById ) ? 1 : 0 ); // Checks whether it is an IE based browser or not.
// Request Handler
var handleMyRequest = function() {
var seq = {
4 : "complete",
complete : 4 };
if ( seq[ this.readyState ] ) { // Request state my hold (STRING/INTEGER) value.
if ( this.status === 200 || this.status === 304 ) { /* The request status handling can be turned off when you are working off-line. */
(( ie ) ? document.all.greenBox : document.getElementById( "greenBox" )).innerHTML = this.responseText;
}
}
};
// Request Builder
var XHR = function( method, url, callback ) {
this.createRequest = null;
this.xml = 0;
this.method = method || "GET";
this.url = url || null;
this.callback = callback || null;
if ( arguments.length < 3 ) {
return false;
} this.xmlDoc = ( function() {
try {
if ( !ie ) {
this.xml = new XMLHttpRequest();
} else {
try {
var client = [ "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP" ];
for ( var i = 0; i < client.length; ++i ) {
if ( new ActiveXObject( client[ i ] )) {
this.xml = new ActiveXObject( client[ i ] );
break;
}
}
} catch( e ) {
this.xml = 0;
}
}
} catch( er ) {
if ( "createRequest" in window )
this.xml = createRequest();
else
this.xml = 0;
} return (( this.xml ) ? this.xml : 0 );
} )();
if ( this.xmlDoc ) {
this.createRequest = ( function( xhr, callback, url, method ) {
(( "overrideMimeType" in xhr ) ? xhr.overrideMimeType("text/xml") : xhr );
xhr.onreadystatechange = callback;
xhr.open( method, encodeURI( url ), true );
(( method === "POST" && "setRequestHeader" in xhr ) ? xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8;") : xhr );
xhr.send((( method === "POST" ) ? " " : null ));
} )( this.xmlDoc, this.callback, this.url, this.method );
} return this.createRequest;
}; onload = function() {
var loc = ""; // Your given value or reference;
var url = "pageserver.php?loc=" + loc;
new XHR( "GET", url, handleMyRequest ); // Building AJAX Call, via onload event.
// The call can be manipulated on any event.
// You can create new instance of request by calling out, the same procedure above.
};
// ]]>
</script>
i can still provide more enhancement over the code, but for now, i think that would enough to handle your trouble.
Hope it helps...essential