Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > OOP (and the XMLHttpRequest)

Reply
Thread Tools

OOP (and the XMLHttpRequest)

 
 
Martin Honnen
Guest
Posts: n/a
 
      12-20-2004


Börni wrote:


> The problem: I assigned a function to xmlhttp.onreadystatechange, which
> (for now) only alerts the response from an php script. BUT in the
> function i always get an error saying xmlhttp has no properties?!



> function xmlRequest() {
> // check if request is already open
> this.xmlhttp=false;
> if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
> this.xmlhttp = new XMLHttpRequest();
> }
> }
>
> 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=ISO-8859-1");
> this.xmlhttp.onreadystatechange = this.getResponse;


Here you set the onreadystatechange handler to a function, when the
onreadystatechange handler is then called inside of the function the
'this' object is the XMLHttpRequest object so

> xmlRequest.method('getResponse', function () {
> if (this.xmlhttp.readyState==4) {
> alert(this.xmlhttp.readyState)


here you need to access
this.readyState

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Börni
Guest
Posts: n/a
 
      12-20-2004
Dear group,

i have some experience with javascript, but now i am trying to do object
oriented programming with it and i am somehow stuck.
I have tried the code so far only in Mozilla 1.7.3
I am trying to make an object which offers the functionaltity of the
xmlhttpRequest object ( my object is something like a wrapper ). the
constructor makes an new XMHttpequest object, and then there are methods
to POST or GET data.
The problem: I assigned a function to xmlhttp.onreadystatechange, which
(for now) only alerts the response from an php script. BUT in the
function i always get an error saying xmlhttp has no properties?!

thanks in advance (sorry for the bad english)
Bernhard

-
function xmlRequest() {
// check if request is already open
this.xmlhttp=false;
if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
this.xmlhttp = new XMLHttpRequest();
}
}

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=ISO-8859-1");
this.xmlhttp.onreadystatechange = this.getResponse;

this.content = "content=" + this.content;
this.xmlhttp.send(this.content);
});

xmlRequest.method('getResponse', function () {
if (this.xmlhttp.readyState==4) {
alert(this.xmlhttp.readyState)
}
});

if you wonder about the syntax xmlRequest.method, its just some sugar

Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      12-20-2004


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/
 
Reply With Quote
 
Börni
Guest
Posts: n/a
 
      12-20-2004
> here you need to access
> this.readyState


ok, so i tried:
alert(this.readyState);

now the alert contains the text undefined.
 
Reply With Quote
 
Börni
Guest
Posts: n/a
 
      12-20-2004
it seems like the reference in this gets destroyed (or something like
that).
Alerting this in the response function prints out the source of the same.
Alerting this in postVal just says [object OBJECT].
Weird
 
Reply With Quote
 
Börni
Guest
Posts: n/a
 
      12-20-2004
Martin Honnen wrote:

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


Thank you very much!
It works.
I did some experiments on my own and perhaps you are intersted in the
result. i wrote some test code:

-
// gets called by some event handler
function goforit() {
var obj = new testObj();
obj.objFunc();
}

function testObj() {
this.var1 = "blabla";
}

testObj.method('objFunc', function() {
var ref = this.extFunc;
this.extFunc(); // outputs blabla
ref(); // outputs undefined
});

testObj.method('extFunc', function() {
alert(this.var1);
});
-

on a second look seems quite reasonable.

Have a nice day
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OOP Book for beginner? Lynn ASP .Net 2 05-26-2005 06:18 PM
Easy OOP with Perl Leslie Viljoen Perl 0 02-21-2005 01:12 PM
Please help!!! OOP and ASP.Net Tutorial (desperate search) Big Dave ASP .Net 4 09-28-2004 05:57 PM
OOP examples poppy ASP .Net 1 06-09-2004 02:29 PM
Another FAILED n-Tier / OOP Web project....... nospam ASP .Net 52 11-13-2003 06:00 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57