473,804 Members | 3,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OOP (and the XMLHttpRequest)

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.onready statechange, 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=fa lse;
if (!this.xmlhttp && typeof XMLHttpRequest! ='undefined') {
this.xmlhttp = new XMLHttpRequest( );
}
}

xmlRequest.meth od('postVal', function (url, content) {
// variables needed to post
this.url = url;
this.content = content;
// alert(this.xmlh ttp);
// very, very important to set sync to true and to set the request
header!!!
this.xmlhttp.op en("POST", this.url, true);
this.xmlhttp.se tRequestHeader( "Content-Type","applicat ion/x-www-form-urlencoded;
charset=ISO-8859-1");
this.xmlhttp.on readystatechang e = this.getRespons e;

this.content = "content=" + this.content;
this.xmlhttp.se nd(this.content );
});

xmlRequest.meth od('getResponse ', function () {
if (this.xmlhttp.r eadyState==4) {
alert(this.xmlh ttp.readyState)
}
});

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

Function.protot ype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Jul 23 '05 #1
5 1835


Börni wrote:

The problem: I assigned a function to xmlhttp.onready statechange, 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=fa lse;
if (!this.xmlhttp && typeof XMLHttpRequest! ='undefined') {
this.xmlhttp = new XMLHttpRequest( );
}
}

xmlRequest.meth od('postVal', function (url, content) {
// variables needed to post
this.url = url;
this.content = content;
// alert(this.xmlh ttp);
// very, very important to set sync to true and to set the request
header!!!
this.xmlhttp.op en("POST", this.url, true);
this.xmlhttp.se tRequestHeader( "Content-Type","applicat ion/x-www-form-urlencoded;
charset=ISO-8859-1");
this.xmlhttp.on readystatechang e = this.getRespons e;
Here you set the onreadystatecha nge handler to a function, when the
onreadystatecha nge handler is then called inside of the function the
'this' object is the XMLHttpRequest object so
xmlRequest.meth od('getResponse ', function () {
if (this.xmlhttp.r eadyState==4) {
alert(this.xmlh ttp.readyState)


here you need to access
this.readyState

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
> here you need to access
this.readyState


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

now the alert contains the text undefined.
Jul 23 '05 #3
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
Jul 23 '05 #4


Börni wrote:
here you need to access
this.readyState

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

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
onreadystatecha nge handler, in my tests here 'this' is the function itself.
Anyway, with MSIE/Win and MSXML (Microsoft.XMLH TTP) the 'this' object in
the onreadystatecha nge 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.meth od('postVal', function (url, content) {
// variables needed to post
this.url = url;
this.content = content;
// alert(this.xmlh ttp);
// very, very important to set sync to true and to set the request
header!!!
this.xmlhttp.op en("POST", this.url, true);

this.xmlhttp.se tRequestHeader( "Content-Type","applicat ion/x-www-form-urlencoded;
charset=UTF-8");

var myRequest = this;
this.xmlhttp.on readystatechang e = function () {
if (myRequest.xmlh ttp.readyState === 4) {
alert(myRequest .xmlhttp.readyS tate);
}
};

this.content = "content=" + encodeURICompon ent(this.conten t);
this.xmlhttp.se nd(this.content );
});
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #5
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
onreadystatecha nge handler, in my tests here 'this' is the function itself.
Anyway, with MSIE/Win and MSXML (Microsoft.XMLH TTP) the 'this' object in
the onreadystatecha nge 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.meth od('postVal', function (url, content) {
// variables needed to post
this.url = url;
this.content = content;
// alert(this.xmlh ttp);
// very, very important to set sync to true and to set the request
header!!!
this.xmlhttp.op en("POST", this.url, true);

this.xmlhttp.se tRequestHeader( "Content-Type","applicat ion/x-www-form-urlencoded;
charset=UTF-8");

var myRequest = this;
this.xmlhttp.on readystatechang e = function () {
if (myRequest.xmlh ttp.readyState === 4) {
alert(myRequest .xmlhttp.readyS tate);
}
};

this.content = "content=" + encodeURICompon ent(this.conten t);
this.xmlhttp.se nd(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
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
14099
by: Matt Kruse | last post by:
I'm aware of the circular reference memory leak problem with IE/closures. I'm not sure exactly how to resolve it in this situation. Also, Firefox appears to grow its memory size with the same code. So I'm wondering if I'm missing something? My test code is as follows: function myObj() { var req = new Object(); req.temp = 0;
2
1793
by: OraWiz | last post by:
Hi ! I have a problem using XMLHttpRequest (AJAX impl). I am getting the cached data all the time. heres the code. function loadDoc( url )
2
6540
by: dx27s | last post by:
Hi all, I'm working with the XMLHttpRequest object. I receive the following error message: "Permission denied to call method XMLHttpRequest.open" This occurs in Firefox only. IE works fine. From my research so far, it seems like this is a security issue related to the fact that I am trying to access a url on a second server. Both servers are under my control, however. Is there a way to get around this
21
11761
by: Joe Attardi | last post by:
Hey all! I was reading over at the IE Blog the other day http://http://blogs.msdn.com/ie/] and read some interesting, and encouraging news. According to Sunava Dutta, an IE Program Manager, starting in IE7 XMLHttpRequest will be a plain JavaScript object. It will still include the Microsoft.XMLHTTP ActiveX component for compatibility, but IMHO this is a very good shift for IE. What do you think?
6
7300
by: Nathan | last post by:
Can I run two XMLHTTPRequest objects at the same time? Im able to get one to work without problems. If I put a call to a function inside the first ones onreadystatechange function, the 2nd ones readyState is always set to 1 (loading). Ive tried using the same XMLHTTPRequest object, and making a 2nd XMLHTTPRequest object with the same results. Heres my code thats giving me problems... function RefreshChat() {
1
2193
by: 4levels | last post by:
Dear Folks, I stumbled upon a strange behaviour of the XMLHttpRequest.. Maybe I'm just not well informed enough about its possibilities, so could someone please confirm my question? When I put plain javscript in a file that is read-in through a XMLHttpRequest-object, it's like it is totally ignored. Eg. I have the file ajax_include.html with in it's body the following lines <script type="text/javascript" language="javascript">
1
4036
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest technology is implemented on more sites now than ever. Compatibility is no longer an issue (IE, Mozilla and Opera all support it), and the benefits to using it are amazing. There are too many PHP programmers avoiding any
8
11164
dmjpro
by: dmjpro | last post by:
i m generating a xml file through jsp file using AJAX. my response header in jsp file is like this .. <%@ page contentType = "text/xml;charset = WINDOWS-1252"%> when i do XmlHttpRequest.responseText the xml content is displayed but when i do the same thing with XmlHttpRequest.responseXML. what's the problem with XmlHttpRequest.responseXML.. Plz explain XmlHttpRequest.responseXML..
1
1410
by: shotokan99 | last post by:
hi, i wanted to check if the url is existing using xmlhttprequest. my browser is firefox. the code goes like this: window.onload=ini var xr= new XMLHttpRequest(); function ini(){ xr.open("HEAD",
6
2307
by: Patrick Nolan | last post by:
I'm working on cross-platform portability of some javascript. My Macintosh testing platform is rather old. It has Safari 1.3.2 and Internet Explorer 5.2. I got Safari working, but now IE is causing trouble. It chokes on this: if (window.XMLHttpRequest) nameReq = new XMLHttpRequest(); else if (window.ActiveXObject) nameReq = new ActiveXObject("Microsoft.XMLHTTP");
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9157
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7621
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3821
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2993
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.