0
<!DOCTYPE html>
<html>

    <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <title>Google Maps JavaScript API v3 Example: Reverse Geocoding</title>
        <link
        href="https://developers.google.com/maps/documentation/javascript/examples/default.css"
        rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
        <script type="text/javascript">
            var geocoder;

            var variablejs;

            geocoder = new google.maps.Geocoder();

            var input = "41.021355,-96.020508";
            var latlngStr = input.split(",", 2);
            var lat = parseFloat(latlngStr[0]);
            var lng = parseFloat(latlngStr[1]);
            var latlng = new google.maps.LatLng(lat, lng);

            geocoder.geocode({
                'latLng': latlng
            }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[0]) {
                        variablejs = results[0].formatted_address; * * //document.write(variablejs); Not working**
                        *
                        //window.location.href = "http://localhost/test2.php?ad="+ variablejs; Working*
                    } else {
                        alert("No results found");
                    }
                } else {
                    alert("Geocoder failed due to: " + status);
                }
            }); * * document.write(variablejs); * * //writes undefined variable (not working)
        </script>
    </head>

    <body></body>

</html>

The section (//document.write(variablejs); Not working), won't write the variable "variablejs", but when I use the method (//window.location.href = "http://localhost/test2.php?ad="+ variablejs; Working), it will write the url and forward the page passing the variable as a parameter. What I want to do is print that variable in the body of the same HTML. I tried to initialize first the variable then write into the Body but It looks like that section is not accessible like a global variable or something like that. Please help.

2
  • change var variablejs; to var variablejs=""; and you should be good to go Commented Apr 19, 2012 at 7:09
  • Why not putting in the body something like this: <div id="output"></div>, and change document.write(something) to document.getElementById('output').innerHTML = something;? Commented Apr 19, 2012 at 7:23

2 Answers 2

3

Once the document is loaded, you can't use document.write without overwriting everything. You will need to manipulate the DOM, e.g. document.body.innerText=variablejs;. But I'd recommend

var d = document.createElement("div");
d.appendChild(document.createTextNode(variablejs));
document.body.appendChild(d);
Sign up to request clarification or add additional context in comments.

3 Comments

Can you point me, where onload event is triggered before using document.write() in OP's code?
Nowhere, but I suppose geocoder.geocode(params, callback) uses a XMLHttpRequest - usually slower than DOMready.
Wow... This explains also why OP gets "undefined variable"-error at last line.
1

You're assigning value to variablejs in the callback function you're passing to geocoder.geocode(). This means that you're trying to access and write it before the callback has been called and therefore you will not see any value set yet.

Debugging with Firebug or similar will help in these cases.

Also you can't use docuemnt.write like that after the document has been loaded, you'll need to do some DOM-manipulation instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.