Here is a simple test page that shows my error (full code below if you don't want to click: http://suppyo.com/suppyo/ I get the following error: citstat is not defined [Break on this error] document.write(citstat); Code (markup): However, I have the following in a function above: var citstat = getFormattedLocation(); Code (markup): What do I need to do differently to get the citstat variable defined? I've tried adding var citstat; and var citstat = ''; but still get the error? Any help would be appreciated. Thanks, Tim <html> <head> <title>Google Maps JavaScript API v3 Example: Common Loader</title> <script type="text/javascript" src="http://www.google.com/jsapi"></script> <script type="text/javascript"> google.load("maps", "3", {callback: initialize, other_params:"sensor=false"}); function initialize() { // Initialize default values var location = ""; // If ClientLocation was filled in by the loader, use that info instead if (google.loader.ClientLocation) { latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude); var lat = google.loader.ClientLocation.latitude; var lng = google.loader.ClientLocation.longitude; var city = google.loader.ClientLocation.address.city.toUpperCase(); var citstat = getFormattedLocation(); /* var latlngStr = latlng.split(",",2); var lat = parseFloat(latlngStr[0]); var lng = parseFloat(latlngStr[1]); */ location = "Showing IP-based location: <strong>" + getFormattedLocation() + " </strong> " + lat + " is lat " + lng + "is lng " + city + " is city " + citstat + " is state"; } document.getElementById("location").innerHTML = location; document.getElementById("cityandstate").innerHTML = citstat; } function getFormattedLocation() { if (google.loader.ClientLocation.address.country_code == "US" && google.loader.ClientLocation.address.region) { return google.loader.ClientLocation.address.city + ", " + google.loader.ClientLocation.address.region.toUpperCase(); } else { return google.loader.ClientLocation.address.city + ", " + google.loader.ClientLocation.address.country_code; } } </script> </head> <body> <div id="location"></div> <div id="cityandstate"></div> <div>Welcome to <script type="text/javascript"> document.write(citstat); </script> </div> </body> </html> Code (markup):
you have a scoping error. within a function, doing var citstat = getFormattedLocation(); makes citstat as a private variable that only the parent function (or any functions that are respective parents of the function that declares the variable) can access. if you need it to be global, remove the var infront of the declaration or pre-declare it as global from outside the function call itself (in your case, you can var citstat; before the initalization function declaration using global variables is a bad practice and should be avoided, if possible (google on using closures and namespacing to make code less intrusive and use less resources) // scoping example var foo = 1; function bar() { foo++; } function ping() { var foo = 1, privateVar = "hello"; leakedVar = "hola"; } function pong() { foo = 1; } alert(foo); // 1 bar(); // 1+1 alert(foo); // 2 ping(); // 2 and not 1 which remains local in ping(); alert(foo); // still 2. alert(typeof(privateVar)); // not accessible here... alert(leakedVar); // hola pong(); alert(foo); // gone to 1 again. Code (javascript):
quick fix, change: var citstat = getFormattedLocation(); to: citstat = getFormattedLocation(); and add: var citstat; to the top of the script.
dimitar - Thank you very much for your explanation. I appreciate it! camjohnson95 - Thank you for your quick fix, very helpful! Tim