•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 426,412 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,366 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 2091 | Replies: 3
![]() |
•
•
Join Date: Jun 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
in the following script ~this pointer~(self variable) is used to bind
XHR to the object that calls the function. but i am wondering why it
is required. anonymous function assigned to the onreadystatechange
event method could create a closure that would make xmlHttpReq
accessible even XHR is not bound to anything, i've tested this
situation if not made some mistake. XHR does not seem to be destroyed when the function exits as its called asynchronously It does not make sense in this simple code but may be meaningfull in an OO approach as multiple methods access the XHR object. Am i right? So what is the semantic of this usage?
thanks
http://www.degraeve.com/reference/si...ax-example.php
XHR to the object that calls the function. but i am wondering why it
is required. anonymous function assigned to the onreadystatechange
event method could create a closure that would make xmlHttpReq
accessible even XHR is not bound to anything, i've tested this
situation if not made some mistake. XHR does not seem to be destroyed when the function exits as its called asynchronously It does not make sense in this simple code but may be meaningfull in an OO approach as multiple methods access the XHR object. Am i right? So what is the semantic of this usage?
thanks
javascript Syntax (Toggle Plain Text)
function xmlhttpPost(strURL) { var xmlHttpReq = false; var self = this; // Mozilla/Safari if (window.XMLHttpRequest) { self.xmlHttpReq = new XMLHttpRequest(); } // IE else if (window.ActiveXObject) { self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); } self.xmlHttpReq.open('POST', strURL, true); self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x- www-form-urlencoded'); self.xmlHttpReq.onreadystatechange = function() { if (self.xmlHttpReq.readyState == 4) { updatepage(self.xmlHttpReq.responseText); } } self.xmlHttpReq.send(getquerystring()); }
The this pointer is not compulsory. Try it out without the 'this' and it would still work out to be fine. Since this function is executed in the context of some form element (text box in the example given below), 'this' here refers to the element under consideration.
Read this for more on this in javascript.
Read this for more on this in javascript.
javascript Syntax (Toggle Plain Text)
function ajaxFunction(strURL) { var xmlHttpReq = false; var self = this; // Mozilla/Safari if (window.XMLHttpRequest) { self.xmlHttpReq = new XMLHttpRequest(); } // IE else if (window.ActiveXObject) { self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); } self.xmlHttpReq.open('POST', strURL, true); self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x- www-form-urlencoded'); self.xmlHttpReq.onreadystatechange = function() { if (self.xmlHttpReq.readyState == 4) { updatepage(self.xmlHttpReq.responseText); } } self.xmlHttpReq.send(getquerystring()); } <form name="myForm"> Name: <input type="text" onkeyup="ajaxFunction(time.jsp);" name="username" /> Time: <input type="text" name="time" /> </form>
I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
•
•
Join Date: Jun 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
thanks for the answer SOS. but the onkeyup just calls the function in your example code. so -this- refers to the global (window) object, not the textbox. as i stated before i know that it is not required to bind the XHR to some object by using this. however i've remembered when referred to quirksmode article that -this- in the example does not bind XHR to the object which invokes the function, but binds it to the global object (window).
in this case, xmlHttpReq becomes a global variable although not defined as a global one. it is arguable that it would make sense or not.
i finally conclude that this sample is crippled from a working code that everything makes sense but removed parts makes -this- usage not so meaningful in the example code.
in this case, xmlHttpReq becomes a global variable although not defined as a global one. it is arguable that it would make sense or not.
i finally conclude that this sample is crippled from a working code that everything makes sense but removed parts makes -this- usage not so meaningful in the example code.
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 963
Reputation:
Rep Power: 5
Solved Threads: 48
If Javascript's object lifecycle is anything like Java's; objects created using 'new' stay resident even after they go out of the scope of the function that called new, provided something else is referencing that object somewhere.. perhaps, the XMLHTTP objects do some kind of forced binding to a single static controller object whenever open() is called... even if they don't; try:
In Opera, I get 'did something' and then 'hello there'.
There's a very weak binding in that the 'myself' provided in the function created for the ( also asyncrounous ) window.setTimeout( ) function refers to the instantiated 'Thing'. That's clearly enough to keep the object alive for at least until the timeout clicks... I'm sure there is some such similar behind-the-scenes binding going on somewhere in XMLHTTP.open( ), because the XMLHTTP has to be 'connected' (pardon the pun) to something that calls its onreadystatechange when the readystate changes; that will keep it from being GC'd.
javascript Syntax (Toggle Plain Text)
function Thing() { var myself = this; window.setTimeout(function(){myself.say_something();},100); this.say = "hello there"; this.say_something = function() { alert( this.say ); } } function something() { thing = new Thing( ); return; } something( ); alert('did something');
In Opera, I get 'did something' and then 'hello there'.
There's a very weak binding in that the 'myself' provided in the function created for the ( also asyncrounous ) window.setTimeout( ) function refers to the instantiated 'Thing'. That's clearly enough to keep the object alive for at least until the timeout clicks... I'm sure there is some such similar behind-the-scenes binding going on somewhere in XMLHTTP.open( ), because the XMLHTTP has to be 'connected' (pardon the pun) to something that calls its onreadystatechange when the readystate changes; that will keep it from being GC'd.
If it only works in Internet Explorer; it doesn't work.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- *Pointer program problem. (C++)
- How to access a Public Pointer value of a class directly? (C++)
- pointer to member? (C++)
- pointer to some code (C++)
- pointer (C)
- Assign content to a function pointer (C)
- Change Out Your Pointer Scheme (Windows tips 'n' tweaks)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: New Webiste, need info...
- Next Thread: how to send data in POST method using javascript



Linear Mode