PDA

View Full Version : xmlhttprequest object


fci
04-24-2005, 03:59 AM
function XML_request(func) {

var onreadystatechange= func;
this.method = "GET";
this.get = function(url) {

var request = null;
var state_change = function() {
if (request.readyState==4) {
if (request.status==200) {
onreadystatechange(request);
} else {
return null;
}
}
}

try {
request = new XMLHttpRequest();
} catch(e) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
return null;
}
}
if (request===null) {
return null;
}
request.onreadystatechange = state_change;
request.open(this.method, url, true);
request.send(null);
}
}
var req = new XML_request(function(response) {
response = response.responseXML
alert(response.getElementsByTagName('blah')[0].firstChild.data);
});

req.get('index.php?mode=blah&a=response')



i didn't like the examples I found so this is suitable.. I only tested in ie and firefox though.. so let me know what I may be missing.. or criticism in general.

and, some of the code came from:
http://www.codingforums.com/showthread.php?t=30449&highlight=xmlhttprequest

brothercake
05-11-2005, 05:42 PM
Why are you using try() catch() to establish the XMLHttpRequest object?

Alex Vincent
05-11-2005, 05:49 PM
brothercake: XMLHttpRequest isn't always available. Although it's available in Internet Explorer, and by default in Netscape/Mozilla, it's considered an extension. Therefore, the function itself isn't always defined.

That may not be the reason fci had in mind, but...

brothercake
05-11-2005, 07:53 PM
Yes but .. surely try() catch() is a debugging construct, and not something you should use in production code, because it's far less efficient than just testing objects yourself, ergo:

if(typeof window.ActiveXObject != 'undefined')
{
var request = new ActiveXObject('Microsoft.XMLHTTP');
}

if(typeof window.XMLHttpRequest != 'undefined')
{
request = new XMLHttpRequest();
}

if(typeof request != 'undefined')
{
//and so on ...
}

The only time you would need try() catch() is if an object were untestable without attempting to instantiate it, but when is that ever the case?

fci
05-12-2005, 01:48 AM
I was actually copying code from the link in my post.
I'm not totally satisfied with the code so I was going to write a newer version(actually started a bit yesterday but other stuff came up) something like this:
if (window.XMLHttpRequest) {
//blah
} else if (window.ActiveXObject) {
//
}
return false


doesn't it return false if it's undefined(seems ok in firefox/ie.. don't know about the rest)? and for the activexobject, I vaguely looked around the msdn site to see if there was a way to test if it was available without the try/catch but that doesn't seem to be possible but I wasn't looking that hard.
was also going to test for all of these as well:
["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]
which I found at sarissa.sf.net ( http://sarissa.sourceforge.net/doc/overview-summary-sarissa.js.html )

enumerator
05-12-2005, 01:56 AM
Yeah, you get an error if the progid is not found, or the user disables/cancels ActiveX; another example... (http://codingforums.com/showthread.php?t=56091)

brothercake
05-12-2005, 05:26 AM
Oh yeah ... ActiveX being disabled - I'd forgotten about that.

So in fact is that the very thing - the example of an object that's untestable without attempting to instantiate it?

enumerator
05-12-2005, 05:32 AM
I think so; "late binding" is what they call it...

brothercake
05-12-2005, 06:04 AM
I think so; "late binding" is what they call it...
What "late" as in "the late bill gates" ?

enumerator
05-12-2005, 06:15 AM
As opposed to "early"; run-time vs. design-time. ;)