J
Jeff
Hello,
I assigned a new object to a local variable ("req") in a function (see
below). The local variable "req" is obviously destroyed when the
function exits, but should the object referenced by the variable live
on?
It seems that it does (and I want it to), but is this correct? I
thought that local variables (and objects) are destroyed when the
function exits? I declared a callback function--function () {
processReqChange (req); };--with the variable that is called later and
it all seems to work okay.
Thank you!
function SendRequest(site,size,position,id) {
var msg="targetsite=" + encodeURI(site)+ "&id=" + encodeURI (id)+
"&targetsize=" + encodeURI (size)+"&position=" + encodeURI(position);
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) { // Safari, Mozilla
var req = new XMLHttpRequest(); //create new local object
if (req.overrideMimeType) {
//set type accordingly to anticipated content type
//req.overrideMimeType('text/xml');
req.overrideMimeType('text/html');
}
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
var req = new ActiveXObject("Microsoft.XMLHTTP"); //create new
local object
}
else {
alert("Your browser does not support XMLHTTP")
}
if (req) {
var url_to_use=url+'?unique='+ new Date().getTime ();
req.open('POST', url_to_use, true); //Open before assign callback to
ensure IE ability to repeat process
req.onreadystatechange = function () { processReqChange (req); };
//Make a new function that calls processReqChange with this object
req.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", msg.length);
req.setRequestHeader("Connection", "close");
req.send(msg);
}
}
I assigned a new object to a local variable ("req") in a function (see
below). The local variable "req" is obviously destroyed when the
function exits, but should the object referenced by the variable live
on?
It seems that it does (and I want it to), but is this correct? I
thought that local variables (and objects) are destroyed when the
function exits? I declared a callback function--function () {
processReqChange (req); };--with the variable that is called later and
it all seems to work okay.
Thank you!
function SendRequest(site,size,position,id) {
var msg="targetsite=" + encodeURI(site)+ "&id=" + encodeURI (id)+
"&targetsize=" + encodeURI (size)+"&position=" + encodeURI(position);
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) { // Safari, Mozilla
var req = new XMLHttpRequest(); //create new local object
if (req.overrideMimeType) {
//set type accordingly to anticipated content type
//req.overrideMimeType('text/xml');
req.overrideMimeType('text/html');
}
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
var req = new ActiveXObject("Microsoft.XMLHTTP"); //create new
local object
}
else {
alert("Your browser does not support XMLHTTP")
}
if (req) {
var url_to_use=url+'?unique='+ new Date().getTime ();
req.open('POST', url_to_use, true); //Open before assign callback to
ensure IE ability to repeat process
req.onreadystatechange = function () { processReqChange (req); };
//Make a new function that calls processReqChange with this object
req.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", msg.length);
req.setRequestHeader("Connection", "close");
req.send(msg);
}
}