Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

Access variable in another function

I am trying to access a value that is inside another function. In the example provided I have declared cjs as a global, but it will not provide a value in my alert?

I suspect the problem is that the value is defined inside of geocoder.geocode(

Not sure how exactly to get to that
geocoder = new google.maps.Geocoder();
	var cjs;
	geocoder.geocode( { 'address': address}, function(results, status) { 
	
	
	  if (status == google.maps.GeocoderStatus.OK) {
		  //alert(status);
		   theCoords = results[0].geometry.location;
		   cjs = "hi";
	  } else {
		alert("Please double check to make sure you entered the zip code correctly.");
	  } 
	});
	alert(cjs)

Open in new window

0
lvmllc
Asked:
lvmllc
  • 4
  • 3
  • 3
  • +1
1 Solution
 
cmalakarCommented:
I am not sure above Google maps..

But in general javascript  the following attached works.

make sure you >>cjs="hi" is actually being executed.



function() {
        var x;
        xyz = function(){
          x = 2;
        };      
        alert("before invoking the xyz function, x is not initialized yet:" + x);
        xyz();  //invoke function xyz, changes the x value
        alert("Here x value would have changed:" + x); 
    }

Open in new window

0
 
dejaanbuCommented:
if (status == google.maps.GeocoderStatus.OK)  --- this line is not executed means u didnt get a success msg.
0
 
cmalakarCommented:
I believe, the function declared inside gecode itself is not being executed.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
i can't see a global cys just after starting the function at line 3 ?
And you'd better define cjs at first line ... i mean change order of first 2 lines.
0
 
lvmllcAuthor Commented:
when I uncomment //alert(status);

It does alert OK for me so I presume that cjs is getting set to 'hi'


I was wondeirng if the problem is that there is a function inside a function?
0
 
cmalakarCommented:
There are two alerts in above code.. one at line 7 and other at line 14.

Which comes first ?
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
did you add globas as i'd mentioned above as the code below


geocoder = new google.maps.Geocoder();
        var cjs;
        geocoder.geocode( { 'address': address}, function(results, status) { 
          
         global cys;
          if (status == google.maps.GeocoderStatus.OK) {
                  //alert(status);
                   theCoords = results[0].geometry.location;
                   cjs = "hi";
          } else {
                alert("Please double check to make sure you entered the zip code correctly.");
          } 
        });
        alert(cjs)

Open in new window

0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
Are there any result?
0
 
lvmllcAuthor Commented:
I am using the globals.

what it appears to be doing is sending the return true before the variable gets a chance to get set.  I have been able to test this.

I also tested it by trying to put the value into a field before the return true was executed and the field never changed.

When I set it to return false it worked fine.

so I wonder is there a way to slightly delay the script until the field or variable receives the value  - then return true.
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
you may try using jquery delay() or setTimeOut()
0
 
lvmllcAuthor Commented:
Solution not complete only because I have had to use work around to get it close to functioning.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now