Here is an interesting observation: when I assign an anonymous function to the onreadystatechange variable, everything works fine. However, if I try to assign a named function to this variable, it does not work as expected.
<script language="Javascript">
function postRequest(strURL)
{
var xmlHttp;
if (window.XMLHttpRequest) // Mozilla, Safari, ...
var xmlHttp = new XMLHttpRequest();
else if (window.ActiveXObject) // IE
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open('POST', strURL, true);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.onreadystatechange = function()
{
alert("you are in anonymous");
}
xmlHttp.send(strURL);
}
</script>
The code above functions correctly, but the following code snippet does not work as intended. The function foo()
does not get called:
<script language="Javascript">
function foo()
{
alert("you are in foo");
}
/*----------------------------------------------*/
function postRequest(strURL)
{
var xmlHttp;
if (window.XMLHttpRequest) // Mozilla, Safari, ...
var xmlHttp = new XMLHttpRequest();
else if (window.ActiveXObject) // IE
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open('POST', strURL, true);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.onreadystatechange = foo;
xmlHttp.send(strURL);
}
</script>
Why do you think that is?