On Nov 29, 2007, at 9:21 AM, Eric Lease Morgan wrote:
> Why doesn't my httpRequest Javascript function return unless I add
> an alert? Grrr.
I have resolved my problem, but I'm not exactly sure how.
First of all, my httpRequest (XMLHttpRequest) code was just fine. I
made no significant changes to it. Instead, I separated my form input/
validation routine from the httpRequest functionality and the problem
disappeared. Don't ask my why. I don't know. This makes for better
modular programing though. javascript--
BTW, I appreciate the links to various Javascript libraries, but
since I am really only starting out in this regard I think I need to
get my hands dirtier before I lean on someone else's code.
Finally, for posterity's sake, I have included my resulting code in
an attachment to this message. I don't know whether or not the list
will accept attachments.
--
Eric Lease Morgan
University Libraries of Notre Dame
(574) 631-8604
function add_tag( theform ) {
// get the form's input
var resource = theform.resource.value;
var tag = theform.tag.value;
var username = theform.username.value;
// process the input
post_tag ( resource, tag, username );
// cleanup and done
expand( 'd' + resource );
return false;
}
function post_tag( resource, tag, username ) {
// use the input to create a GET request
var url = './index.cgi?cmd=post_tag&resource=' + resource + '&tag=' + tag + '&username=' + username;
// create a xmlRequest
var xmlRequest;
if ( window.XMLHttpRequest ) { xmlRequest = new XMLHttpRequest(); }
else if ( window.ActiveXObject ) { xmlRequest = new ActiveXObject( "Microsoft.XMLHTTP" ); }
// sanity check
if ( !xmlRequest ) {
alert( 'Giving up: Cannot create an XMLHTTP instance' );
return false;
}
// give the xmlRequest some characteristics and send it off
xmlRequest.open( 'GET', url, true );
xmlRequest.send( null );
xmlRequest.onreadystatechange = function() {
if ( xmlRequest.readyState == 4 ) {
var xmldoc = xmlRequest.responseXML;
var root_node = xmldoc.getElementsByTagName( 'root' ).item( 0 );
alert ( root_node.firstChild.data );
}
}
}
function expand( id ) {
var details = document.getElementById( id );
details.style.display = ( details.style.display == 'block' ) ? 'none' : 'block';
}
|