Thursday, December 15, 2005

fun with XMLHttpRequest

Now I want to read the sf logo asynchronous using XMLHttpRequest, mostly just to get familiar with it.

First, I want an asend method that I can invoke like this:

asend('get','/sflogo.html',loadlogo)

This says do a get asyncronously to /sflogo.html, and call loadlogo with the response.

function loadlogo(ro){
document.getElementById('sflogo').innerHTML=ro.responseText
}

This approach gives me pretty good code reuse. Here's my first draft of asend:

function asend(mode,action,go){
var ro;
var browser=navigator.appName;
if(browser=="Microsoft Internet Explorer"){
ro=new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro=new XMLHttpRequest();
}
ro.open(mode,action);
ro.onreadystatechange=check
ro.send(null)
return true;

function check(){
if (ro.readyState==4){
go(ro)
}
}
}

Probably a few improvements are needed. But it works for now.

I did find that the target element needs to be a div, not an iframe. Nice thing is, div looks better for this application.

0 Comments:

Post a Comment

<< Home