Jump to content

I always have this problem with javascript scope


Shadowing
 Share

Recommended Posts

Hey guys this is driving me crazy i dont know why i cant simply return a value like i would in a php functionI can never read out side ajax brackets, the script isnt failing eitherrank alerts "0" on success says rank is undefined

 function grab_alliance_rank() {     $.ajax({		      url: "ajax/politics.php?functions=rank",		      type: 'POST',		      dataType: 'json',		      error: function(){ 	alert("Oops... seems to be a problem retrieving data. function alliance rank") 		},	success: function(response) {		var rank = (response.rank);								 alert(rank); // this works here	}		  });return rank ;  } 

Link to comment
Share on other sites

scope is not the problem here, when the ajax function is called it goes away to do its thing, but the' return rank;' kicks off before response is returned. The use of async: false, fixes this, because it forces the script to pause and wait for a response, then continue and so the return has a value to return. Problem: async: false is deprecated, and you are advised not to use this as i said it causes delay while it waits for returned response, this sometimes causes a locking of the page especially for older browsers. Solution: use callback function to retrieve response example

function grab_alliance_rank(rank) {  $.ajax({			        url: "ajax/politics.php?functions=rank",			       type: 'POST',				      dataType: 'json',					      error: function(){	    alert("Oops... seems to be a problem retrieving data. function alliance rank")	    	    },	    success: function(response) {	    rank(response.rank);	    }  });}

<div id="puthere"></div><script>grab_alliance_rank(function(getrank){document.getElementById("puthere").innerHTML=getrank;})</script>

Link to comment
Share on other sites

Thanks for the reply as always dsonesuk im a little confused lol this below is how im grabing the rank

grab_alliance_rank(function(getrank){document.getElementById("puthere").innerHTML=getrank;})

how am I storing the rank? dont i have to put something here to store it

	success: function(response) {		var rank = (response.rank);	  // store rank some how here	} 

and dont I need a return on grab_alliance_rank function

function grab_alliance_rank(rank) {    $.ajax({	 // grabs the users alliance rank	     url: "ajax/politics.php?functions=rank",		     type: 'POST',		     dataType: 'json',		     error: function(){	alert("Oops... seems to be a problem retrieving data. function alliance rank")		},	success: function(response) {		var rank = (response.rank);	   // store rank some how here	}		  });     return rank;} 

so I guess this some how inserts the agrument?document.getelementbyid works as a print or echo sort of speak?

function(getrank){document.getElementById("puthere").innerHTML=getrank;}

Edited by Shadowing
Link to comment
Share on other sites

Look at how function grab_alliance_rank sends another function as a argument as you would in normal circumstance when you would use

function rank(getrank){document.getElementById("puthere").innerHTML=getrank;}

with a call of function

rank("whatever rank")

you end up with "whatever rank" displayed in element with id "puthere" You are basically doing the same where 'rank' in 'grab_alliance_rank(rank)' becomes the reference to function you sent as argument that you will call to send the returned response back wiith, where 'getrank' in function(getrank) will be the value of "whatever rank".function grab_alliance_rank(rank) { $.ajax({ url: "ajax/politics.php?functions=rank", type: 'POST', dataType: 'json', error: function(){ alert("Oops... seems to be a problem retrieving data. function alliance rank") }, success: function(response) { rank(response.rank); } });}<div id="puthere"></div><script>grab_alliance_rank(function(getrank){document.getElementById("puthere").innerHTML=getrank;})

Link to comment
Share on other sites

omg dsonesuk lol I think i have to give you more code in how im using it cause im still clueless, then you can see my error and give me a jab in the face.

function politics() { // trying to read the rank in this function   rank = grab_alliance_rank(  	function(getrank)	{	 document.getElementById("alliance_rank").innerHTML=getrank;	}  )alert(rank);  } 

function grab_alliance_rank(rank) {    $.ajax({	 // grabs the users alliance rank	     url: "ajax/politics.php?functions=rank",		     type: 'POST',		     dataType: 'json',		     error: function(){	alert("Oops... seems to be a problem retrieving data. function alliance rank")		},	success: function(response) {		var rank = (response.rank);	}  });  }

<div id="alliance_rank"></div>

Edited by Shadowing
Link to comment
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script><script type="text/javascript">/*<![CDATA[*//*---->*/function grab_alliance_rank(rank) {  $.ajax({			        url: "politics.php?functions=rank",			       type: 'POST',				      dataType: 'json',					     error: function(){	    alert("Oops... seems to be a problem retrieving data. function alliance rank")	    	    },	    success: function(response) {	    rank(response.rank);	    }  });}/*--*//*]]>*/</script><style type="text/css"></style></head><body><div id="alliance_rank"></div><script>function politics()    { // trying to read the rank in this function       grab_alliance_rank(       function(rank)        {		 document.getElementById("alliance_rank").innerHTML=rank;         alert(rank);	    })    }politics()</script></body></html>

Link to comment
Share on other sites

I forget, I have this magical pc which means any code i run in any browser works! and you poor minions have to deal with your poor pathetic substandard O/S, OR you have some conflict in the code, or you are doing it in such away that it is failing to achieve its intended end result. and no i'm not going to dump my pc in the hot volcanic pool of mount doom.

Link to comment
Share on other sites

lol dosonesuk. well it would explain alot :) and all this time i thought you was a coding god when it is really just your OS if I do this alert doesnt even pop up

 $(function(){ politics();  function politics() {    grab_alliance_rank(   	function(rank)	{	 document.getElementById("alliance_rank").innerHTML=rank;	 			  alert("test"); // doesn't pop up	}); 			  alert("test"); // does pop up}   });

Link to comment
Share on other sites

 $(function(){function grab_alliance_rank(ThisArgumentMustExist) { // this argument will be used as name ref to function passed as argumentvar thisCommamd = "grunt";ThisArgumentMustExist(thisCommamd);}function politics() { 	     grab_alliance_rank(		 //the function passed as argument start		    function(ThisArgumentMustAlsoExist)		    { 								   alert(ThisArgumentMustAlsoExist); // doesn't pop up  		    } //the function passed as argument end);   										  }    politics();  }); 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...