Why does my variables dont take the value in the function?

江枫思渺然 提交于 2019-12-08 12:31:01

问题


I want to display on screen the var longitude en latitude but I think that the function is executing last and I'am stuck with the initial values. The objective is to print on screen the exact values of the geolocation that the browser returns. Thanks !!!

<!DOCTYPE html>


<head>

<script>
var longitude = "10";
var latitude = "20";
</script>




</head>
<html>
<body  onload="getLocation()">


<script>
var longitude = "30";
function getLocation() {

    if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position){
                latitude = position.coords.latitude;
                longitude = position.coords.longitude;
                alert('aaab:' +longitude);
    });
    }else {
                   alert("Geolocation API is not supported in your browser.");
    }
    }

</script>



<script>
alert("ccc");
document.write (longitude);
document.write (latitude);

</script>
</body>
</html>

i know that it's working within the function, but there is any way to use those variable outside? I just want to be able to store them in one global variable that cand be called wherever. Thanks


回答1:


document.write is being executed before your code that updates it is being run. You need to do the document.write in the callback like so:

<!DOCTYPE html>
  <head>
    <script type='text/javascript'>
      var longitude = "10";
      var latitude = "20";

      function getLocation() {
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position){
            latitude = position.coords.latitude;
            longitude = position.coords.longitude;

            document.write(latitude+" / "+longitude);  
          });
        }else{
          alert("Geolocation API is not supported in your browser.");
        }
      }

    </script>
  </head>
  <body onload="getLocation()">

  </body>
  </html>



回答2:


Try this:

function getLocation() {

    if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(displayLocation);
    }else {
                   alert("Geolocation API is not supported in your browser.");
    }
}

    function displayLocation(position) {
       latitude = position.coords.latitude;
       longitude = position.coords.longitude;
       alert('aaab:' +longitude);
    }



回答3:


The Geolocation Callback probably hasn't been called at the time you write your longitude and latitude. Maybe you can use jQuery to write the correct values to the screen...

<script>
    var longitude = "30";
    function getLocation() {

        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position){
                latitude = position.coords.latitude;
                longitude = position.coords.longitude;
                //alert('aaab:' +longitude);

                // Use jQuery to write your values to the html
                $("#longitude").html(longitude);
                $("#latitude").html(latitude);
            });
        }else {
            alert("Geolocation API is not supported in your browser.");
        }
    }

</script>

<html>
    ...
    <body onload="getLocation()">
        <div id="longitude"></div>
        <div id="latitude"></div>
    </body>
</html>


来源:https://stackoverflow.com/questions/14859113/why-does-my-variables-dont-take-the-value-in-the-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!