Print

Print


To point out why the use of a Javascript framework is important, let me
put your code into jQuery (http://jquery.com)

$.get('index.cgi', {cmd:'add_tag', username:'username'}, function(html) {
  // do whatever you want here
})

PrototypeJS has a similar, easy to use construct. In the end, using any
framework is more important than the specific choice of framework.

>Why doesn't my httpRequest Javascript function return unless I add an
>alert? Grrr.
>
>I am writing my first AJAX-y function called add_tag. This is how it
>is suppose to work:
>
>   1. define a username
>   2. create an httpRequest object
>   3. define what it is suppose to happen when it gets a response
>   4. open a connection to the server
>   5. send the request
>
>When the response it is complete is simply echos the username. I know
>the remote CGI script works because the following URL works correctly:
>
>   http://mylibrary.library.nd.edu/demos/tagging/?
>cmd=add_tag&username=fkilgour
>
>My Javascript is below, and it works IF I retain the "alert
>( 'Grrr!' )" line. Once I take the alert out of the picture I get a
>Javascript error "xmldoc has no properties". Here's my code:
>
>
>   function add_tag() {
>
>    // define username
>    var username  = 'fkilgour';
>
>    // create an httpRequest
>    var httpRequest;
>    if ( window.XMLHttpRequest ) { httpRequest = new XMLHttpRequest(); }
>    else if ( window.ActiveXObject ) { httpRequest = new ActiveXObject
>( "Microsoft.XMLHTTP" ); }
>
>    // give the httpRequest some characteristics and send it off
>    httpRequest.onreadystatechange = function() {
>
>     if ( httpRequest.readyState == 4 ) {
>
>      var xmldoc = httpRequest.responseXML;
>      var root_node = xmldoc.getElementsByTagName( 'root' ).item( 0 );
>      alert ( root_node.firstChild.data );
>
>     }
>
>    };
>
>    httpRequest.open( 'GET', './index.cgi?cmd=add_tag&username=' +
>username, true );
>    httpRequest.send( '' );
>    alert ( 'Grrr!' );
>
>   }
>
>
>What am I doing wrong? Why do I seem to need a pause at the end of my
>add_tag function? I know the anonymous function -- function() -- is
>getting executed because I can insert other httpRequest.readyState
>checks into the function and they return. Grrr.
>
>--
>Eric Lease Morgan
>University Libraries of Notre Dame
>
>(574) 631-8604