Can anyone explain if there is a difference here?
I'm lost, Thanks.
I have a class that works...
function CXMLHTTP()
{
var xmlhttp;
//this.m_xmlhttp;
this.url = "";
this.m_Response = "";
this.init = init;
this.submit = submit;
this.onReadyStateChange = onReadyStateChange;
alert( "CXMLHTTP Constructor" );
function init()
{
try
{
// Firefox, Opera 8.0+, Safari
xmlhttp = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
}
function onReadyStateChange()
{
if( xmlhttp.readyState == 4 )
{
this.m_Response = xmlhttp.responseText;
alert( "Response = " + this.m_Response );
}
}
function submit( myurl )
{
alert( "start submit" );
xmlhttp.open("POST", myurl, true);
xmlhttp.send(null);
alert( "end submit" );
}
this.init();
xmlhttp.onreadystatechange = this.onReadyStateChange;
}
But if I change it, as I'd like to, to this
( note that the var xmlhttp is now this.m_xmlhttp )
function CXMLHTTP()
{
//var xmlhttp;
this.m_xmlhttp;
this.url = "";
this.m_Response = "";
this.init = init;
this.submit = submit;
this.onReadyStateChange = onReadyStateChange;
alert( "CXMLHTTP Constructor" );
function init()
{
try
{
// Firefox, Opera 8.0+, Safari
this.m_xmlhttp = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
this.m_xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
this.m_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
}
function onReadyStateChange()
{
if( this.m_xmlhttp.readyState == 4 )
{
this.m_Response = this.m_xmlhttp.responseText;
alert( "Response = " + this.m_Response );
}
}
function submit( myurl )
{
alert( "start submit" );
this.m_xmlhttp.open("POST", myurl, true);
this.m_xmlhttp.send(null);
alert( "end submit" );
}
this.init();
this.m_xmlhttp.onreadystatechange = this.onReadyStateChange;
}
I get these errors.
if( this.m_xmlhttp.readyState == 4 )
for the line in onReadyStateChange
Error: this.m_xmlhttp has no properties
Source File: http://localhost/js/CXMLHTTP.js
Line: 47
Why isn't this working?
Is there something special about the onReadyStateChange callback?
does using var change the scope of the variable for this class
somehow?
It all seems ok, except in the callback function.
Thanks