【问题标题】:Why does my variables dont take the value in the function?为什么我的变量不取函数中的值?
【发布时间】:2013-02-13 17:15:14
【问题描述】:

我想在屏幕上显示 var longitude en latitude 但我认为该函数是最后执行的,并且我坚持使用初始值。 目标是在屏幕上打印浏览器返回的地理位置的确切值。 谢谢!!!

<!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>

我知道它在函数内工作,但有什么方法可以在外部使用这些变量?我只想能够将它们存储在一个可以在任何地方调用的全局变量中。谢谢

【问题讨论】:

    标签: javascript jquery html function geolocation


    【解决方案1】:

    document.write 在更新它的代码运行之前被执行。您需要像这样在回调中执行 document.write:

    <!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>
    

    【讨论】:

    • 我知道它在函数内工作,但是有什么方法可以在外面使用这些变量吗?我只想能够将它们存储在一个可以在任何地方调用的全局变量中。谢谢
    • 全局变量被设置为纬度和经度。您的问题很简单,直到您的原始 document.write 运行之后才会发生。为了清楚起见:jsfiddle.net/Asvjg
    • 好的,谢谢,我明白了
    【解决方案2】:

    试试这个:

    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】:

      在您编写经度和纬度时,可能尚未调用地理位置回调。也许您可以使用 jQuery 将正确的值写入屏幕...

      <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>
      

      【讨论】:

      • 正确,对getCurrentPosition() 的调用是异步的。浏览器使用一些线索(例如您的 IP 地址)来查询地理定位服务(例如 Google)。一个快速的解决方案是将您的 document.write() 语句放在您定义的回调中。我将编辑此答案以显示这一点。我自己打字,但@Syjin 打败了我 :)
      • 你又打败了我,先生 :)
      • 我知道它像你说的那样工作,我有“ alert('aaab:' +longitude); ”工作正常,但我需要在函数之外使用这些变量。
      • 你可以在任何你喜欢的地方使用它们。你只需要等到getCurrentPosition() 被调用。
      • 是的,我想,但是,我不知道该怎么做,页面首先执行正文,然后执行功能。
      猜你喜欢
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      相关资源
      最近更新 更多