Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Java script function returning undefined

    I have a function where in I call another function that assigns a value to a var.

    e.g.
    Code:
    function setVar{
       var thisVar = 'hello';
       return thisVar;
    }
    function callFuncSetVar {
       var newVar = setVar();
      alert(newVar);
    }
    For some reason my code below is returning 'undefined'. But when I place an alert(); before the 'return' it shows the correct value. However when I place an alert(); to show the var that is set to what the function returns it says 'undefined'.

    Firebug throws no errors. I am using a little bit of jQuery.
    Last edited by Kor; 10-21-2010 at 10:30 AM. Reason: wrap the code [code][/code]

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    function setVar{
    var thisVar = 'hello';
    return thisVar;
    }
    function callFuncSetVar {
    var newVar = setVar();
    alert(newVar);
    }
    That should be
    Code:
    function setVar(){
    var thisVar = 'hello';
    return thisVar;
    }
    function callFuncSetVar (){
    var newVar = setVar();
    alert(newVar);
    }
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    <script type = "text/javascript">

    function setVar() {
    var thisVar = 'hello';
    return thisVar;
    }

    function callFuncSetVar() {
    var newVar = setVar();
    alert(newVar); // hello
    }

    callFuncSetVar();

    </script>

    Aarrgghh! abduraooft beat me to it.

  • #4
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    That should be
    Code:
    function setVar(){
    var thisVar = 'hello';
    return thisVar;
    }
    function callFuncSetVar (){
    var newVar = setVar();
    alert(newVar);
    }
    Hey sorry,
    That was a typo. I have mentioned the brackets in ma original code.... Any other solution please

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    That code (and the same code in Post#2) works just fine for me in all browsers.

  • #6
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is my real code

    Code:
    function mobile_validate(){ 
    	
    	  var j=/^[0-9]+$/;
    	  if(document.getElementById('mobile').value.match(j)){
    	  }else{
    		  alert('Please enter only Number.');
    		  document.getElementById('mobile').focus();
    		  return false;
    	 }
    	 if(document.getElementById("mobile").value.indexOf("0")==0){
    		 alert("Please do not use 0 at the begining of the mobile number.");
    		 document.getElementById("mobile").focus();
    		 return false;
    	 }
    	 var k=document.getElementById("mobile").value.charAt(0);
    	 if(k!=9&&k!=8&&k!=7){
    		 alert("Your mobile number should begin either with 7 or 8 or 9");
    		 document.getElementById("mobile").value ="";
    		 document.getElementById("mobile").focus();
    		 return false;
    	 }
    	 m = document.getElementById('mobile').value.length;
    	 if(m>0&&m!=10){
    		 alert("Please enter your 10 digit mobile number.");
    		 document.getElementById("mobile").value ="";
    		 document.getElementById("mobile").focus();
    		 return false;
    	 }
    	var mobile_Request;  
    	var mobile_val;
    	mobile_Request = new GetXmlHttpObject();
    	mobile_Request.onreadystatechange = function(){
    		if(mobile_Request.readyState == 4){
    			var mobileDisplay = mobile_Request.responseText;
    			if(mobileDisplay == '2'){
    				document.getElementById('mobile_err').innerHTML = "<b>Mobile already exists.</b>";
    				//mobile_val = "1";		//alert(mobile_val);
    				return 1;
    			}else{
    				document.getElementById('mobile_err').innerHTML = "<span style='padding:0px;'><img src='images/right1.jpg'/></span>";
    				mobile_val = 0;
    				return mobile_val;
    			}
    		}
    	}
    
    	 var mobile    =  document.getElementById('mobile').value;
    	 var para_mobile = "?mobile="+mobile;
    	 mobile_Request.open("GET", "get_mobile.php"+para_mobile, true);
    	 mobile_Request.send(null); 
    	
    }
    
    
    function ajaxFunction() {
    
     var mv = mobile_validate();
    alert(mv);
    }
    Here alerting the value of mv is giving me "undefined" value
    Please help me with this
    Last edited by Kor; 10-21-2010 at 10:31 AM. Reason: Will you, please end the code with [/code]? Thank you!

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Stripped down it still seems to work:-

    Code:
    <script type = "text/javascript">
    function mobile_validate(){ 
    var mobile_val;
    mobile_val = "1"; 
    alert (mobile_val);
    return mobile_val;
    }
    
    function ajaxFunction() {
    var mv = mobile_validate();
    alert ("MV = " + mv);
    }
    
    
    ajaxFunction();
    </script>
    What does Firebug say?

  • #8
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Philip, The above script worked fine. But my mobile_validate is an ajax function. And my original script is still returning "undefined"

  • #9
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Quote Originally Posted by pratik.itworld View Post
    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.

  • #10
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Ohhh! You are using asynchronous request. This means that the outer function will never return something else excepting 'undefined'.

  • #11
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Thanks for your solutions guys...
    But i resolved the problem.
    The issue was.. as I m sending the ajaxRequest.send(null) it is making all the variables undefined...
    so i returned the value aftr the ajaxRequest.send(null) and it startd working..


  • #12
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Solution

    You have to put the onreadystatechange function after the send request, and you ALSO have to set the open() third parameter to false, to make it synchronous.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •