Re: [CODE4LIB] httpRequest javascript.... grrr [resolved]

2007-11-29 Thread Eric Lease Morgan


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_tagresource=' + 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';

}










Re: [CODE4LIB] httpRequest javascript.... grrr [resolved]

2007-11-29 Thread pkeane

Indeed, my proposed fix was incorrect -- an alert does NOT need to be
passed into the function as a callback (it's always globally available)
and since the parameter for the alert is response text, that's A-OK.  If
you want to insert that response into the page (and not just 'alert' it),
you WOULD need to create a callback function which made reference to the
page element 'target' (thus serving as a closure).

One thing about XHR -- you have all four HTTP verbs at your disposal: GET,
POST, PUT, DELETE and so you may wish to use one of the non-safe (i.e.
state-changing) methods for your XHR call (probably POST in the case of
adding a tag) to make things more RESTful.  XHR is actually a very good
way to hijack links (which are otherwise going to simply use 'GET')
which will perform state-changing operations.  Then when your application
starts exposing web services, you'll be that much more aligned with
RESTful principles (I'm convinced that's v. important, although plenty of
successful services expose unsafe GETs).  Just a thought...

best-
Peter Keane


On Thu, 29 Nov 2007, Eric Lease Morgan wrote:



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