Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-20-2010, 04:18 PM   PM User | #1
pratik.itworld
New to the CF scene

 
Join Date: Oct 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
pratik.itworld is an unknown quantity at this point
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 09:30 AM.. Reason: wrap the code [code][/code]
pratik.itworld is offline   Reply With Quote
Old 10-20-2010, 04:23 PM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
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);
}
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 10-20-2010, 04:24 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
<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.
Philip M is offline   Reply With Quote
Old 10-20-2010, 04:27 PM   PM User | #4
pratik.itworld
New to the CF scene

 
Join Date: Oct 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
pratik.itworld is an unknown quantity at this point
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
pratik.itworld is offline   Reply With Quote
Old 10-20-2010, 04:31 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
That code (and the same code in Post#2) works just fine for me in all browsers.
Philip M is offline   Reply With Quote
Old 10-20-2010, 04:35 PM   PM User | #6
pratik.itworld
New to the CF scene

 
Join Date: Oct 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
pratik.itworld is an unknown quantity at this point
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 09:31 AM.. Reason: Will you, please end the code with [/code]? Thank you!
pratik.itworld is offline   Reply With Quote
Old 10-20-2010, 05:14 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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?
Philip M is offline   Reply With Quote
Old 10-20-2010, 05:22 PM   PM User | #8
pratik.itworld
New to the CF scene

 
Join Date: Oct 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
pratik.itworld is an unknown quantity at this point
Hi Philip, The above script worked fine. But my mobile_validate is an ajax function. And my original script is still returning "undefined"
pratik.itworld is offline   Reply With Quote
Old 10-20-2010, 05:42 PM   PM User | #9
siberia-man
Regular Coder

 
Join Date: Sep 2010
Location: Far far away
Posts: 122
Thanks: 0
Thanked 16 Times in 16 Posts
siberia-man is on a distinguished road
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.
siberia-man is offline   Reply With Quote
Old 10-20-2010, 05:56 PM   PM User | #10
siberia-man
Regular Coder

 
Join Date: Sep 2010
Location: Far far away
Posts: 122
Thanks: 0
Thanked 16 Times in 16 Posts
siberia-man is on a distinguished road
Ohhh! You are using asynchronous request. This means that the outer function will never return something else excepting 'undefined'.
siberia-man is offline   Reply With Quote
Old 10-20-2010, 06:02 PM   PM User | #11
pratik.itworld
New to the CF scene

 
Join Date: Oct 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
pratik.itworld is an unknown quantity at this point
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..

pratik.itworld is offline   Reply With Quote
Old 12-06-2012, 05:03 AM   PM User | #12
zwhitchcox
New to the CF scene

 
Join Date: Dec 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
zwhitchcox is an unknown quantity at this point
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.
zwhitchcox is offline   Reply With Quote
Reply

Bookmarks

Tags
functions, javascript, undefined

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:53 AM.


Advertisement
Log in to turn off these ads.