[Javascript] XMLHttpRequest help (yeah, yeah, I know)

Matt Warden mwarden at gmail.com
Tue Aug 2 19:25:17 CDT 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mike Stickel wrote:
>> http://www.devx.com/webdev/Article/28695
>>
>> There is an example of simple DOM manipulations based on the XML
>> content received in the downloadable code. If you have questions, let
>> me know.
>>
>> - --
>> Matt Warden
> 
> 
> Matt, this helps a little bit but I'm still confused. I've modified  the
> Apple developer example[1] to work with my data/XML file but I  don't
> want the user to have to click on the lists for the quote to  show. I'd
> like one quote to show up right away and then use next/ previous links
> to change the quotes. I may be way off base in this  type of application
> of Javascript but then again, that's why I'm  asking the questions.

I reference the Apple article just as background. Did you download the
associated code? There is a JavaScript class called RemoteConnection
that would make things a lot easier for you (maybe; it's really
designed for a complex response type, not a number of results of a
single response type).

Here's what I would suggest:

1. Static XML file of quotes on the server, named quotes.xml.
2. In your web page, add a function to the window.onload (there is a
function included in the downloadable code called addBodyOnload() that
will handle adding an arbitrary number of functions to the onload, if
that's a problem).
3. The function in window.onload initiates a request to the server
using XMLHttpRequest (or RemoteConnection) for the quotes.xml file.
4. The quotes.xml file is then accessed via the request.responseXML
property at teh appropriate time (see article).
5. You get a reference to the document element:
var xml = request.responseXML;
var root = xml.documentElement;
6. You get a nodeList (array) of quote elements:
var quotes = root.getElementsByTagName('quote');
7. Display a random quote, by accessing the index randomly. Something
like:
var randomQuote =
   quotes[Math.round(Math.random() * quotes.length)].firstChild;
8. Update the document using DOM with somethign like:
document.getElementById('quotediv').appendChild(document.createTextNode(randomQuote));

Then, with prev/next links, you could simply access a new index of the
array (that you saved, so you dont have to make another request).

This is just off the top of my head. You'll probably have to work with
it a bit. Take a look at the downloadable code. I'm not sure the
method of respnose handling I suggest in the article is best for you,
but the code is a decent example of how to do these operations.

hth,

- --
Matt Warden
Miami University
Oxford, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFC8A7tAQ0d4HGyPE8RAtnIAJ92YYOFjV0gqno1m1ZA2yZyGs4G3gCfUxTn
6YwYy/pigXXevZunwf4b7Us=
=eatE
-----END PGP SIGNATURE-----



More information about the Javascript mailing list