Börni wrote:
>> here you need to access
>> this.readyState
>
>
> ok, so i tried:
> alert(this.readyState);
>
> now the alert contains the text undefined.
It seems Mozilla doesn't set the 'this' object correctly (well at least
as it is the convention in other event handlers) for the
onreadystatechange handler, in my tests here 'this' is the function itself.
Anyway, with MSIE/Win and MSXML (Microsoft.XMLHTTP) the 'this' object in
the onreadystatechange handler isn't the request object either, it is
simply the window object there so your approach doesn't work out if you
go cross-browser.
What you could do instead is making use of a closure e.g.
xmlRequest.method('postVal', function (url, content) {
// variables needed to post
this.url = url;
this.content = content;
// alert(this.xmlhttp);
// very, very important to set sync to true and to set the request
header!!!
this.xmlhttp.open("POST", this.url, true);
this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;
charset=UTF-8");
var myRequest = this;
this.xmlhttp.onreadystatechange = function () {
if (myRequest.xmlhttp.readyState === 4) {
alert(myRequest.xmlhttp.readyState);
}
};
this.content = "content=" + encodeURIComponent(this.content);
this.xmlhttp.send(this.content);
});
--
Martin Honnen
http://JavaScript.FAQTs.com/