[thelist] XML fragment into DOM

Matt Warden mwarden at gmail.com
Wed Jul 13 23:32:51 CDT 2005


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


jeff,

Jeff Howden wrote:
> Rod,
> 
> 
>><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
>>From: raanders at mailporter.net
>>
>>I now have two applications set and ready to use 
>>xmlHTTPRequest to pull data/responses from scripts on
>>servers.  I'm at the point of getting the data in the
>>document.
>>
>>Since I write the scripts on the server I can return it
>>in any format I want.  I see four different ways for
>>one script and three of them for the other.
>><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> 
> 
> Please tell me that you didn't try to write the script for doing the
> XMLHTTPRequest yourself.  I say this because there are several very good
> libraries out there that already abstract the browser differences for you.

The browser differences aren't a problem. This function, which is
available on 13048901892308123 different sites, takes care of it:

var req;

function loadXMLDoc(url) {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send();
	}
}

http://developer.apple.com/internet/webcontent/xmlhttpreq.html

Now, you are correct that libraries abstract this, but who cares? What
they *should* and *don't* abstract is the details of dealing with the
XMLHttpRequest object itself. Or, at least, I have yet to come across
one that does.

>><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
>>My preference is some ( builtin/loadable? ) function to
>>turn an xml fragment into part of the DOM.
>><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> 
> 
> If the fragment contains nothing but standard XHTML, inserting that fragment
> is as easy as finding the element you wish to make the parent of the
> fragment and stuffing it into that element using its innerHTML property.
> Purists will gag reading that and suggest that you loop over the structure
> of the fragment inserting the various pieces using calls to the
> createElement() and appendChild() methods of the document object.

Yes, "purists" will gag at the use of innerHTML, but it's more because
of the mixing of data and presentation than any sadistic desire to see
people using DOM functions for everything.

The purist might also point out that you are obviously fully aware of
the benefits of abstraction, but somehow disregard it for this purpose.

>><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
>>Suggestions or even better pointers ( my searches --
>>five pages into a google search ) didn't help.
>><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> 
> 
> Neuromancer is a *very* robust XHR library.

I had not seen this before. I only looked at it briefly, but it looks
like it might encapsulate the use of XHR pretty well.

- --
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)

iD8DBQFC1eryAQ0d4HGyPE8RAiMgAJ47vfJG8URSvQmunIgN0ADrMgg/8ACffM9g
UbHhSpfII+KtdQaUVYF1OgA=
=RCRq
-----END PGP SIGNATURE-----


More information about the thelist mailing list