Quote:
Originally Posted by pratik.itworld
this is my real code
Code:
function mobile_validate(){
...
var mobile_val;
mobile_request = new getxmlhttpobject();
mobile_request.onreadystatechange = function(){
if(mobile_request.readystate == 4){
var mobiledisplay = mobile_request.responsetext;
if(mobiledisplay == '2'){
...
//mobile_val = "1"; //alert(mobile_val);
return 1;
}else{
...
mobile_val = 0;
return mobile_val;
}
}
}
...
}
...
here alerting the value of mv is giving me "undefined" value
please help me with this
|
Look here! I have removed all insensitive lines from your code and kept essential only. Let's consider them in their order.
1. the internal variable
mobile_val is defined
2. the callback function
mobile_request.onreadystatechange is defined. It will set this variable when readystate will changed to 4 (a document is loaded and ready to use).
3. Other lines attempt to set this variable or return some valie that should be set.
So
mobile_request.onreadystatechange does not return any value. It is used to make somthing on different stages of a document loading. In your case it should set the variable
mobile_val = something
After that the outer function
mobile_validate should return this value like this --
return mobile_val;.
As a conclusion. your function can look like below:
Code:
function mobile_validate()
{
var return mobile_val;
...
mobile_request.onreadystatechange = function()
{
....
mobile_val = ...
...
}
...
return mobile_val;
}
You understand that I show the essential lines only. I have many critical points over this code related optimality of it's algorithm but there will be so many words so recently I keep your attention to the main problem.