最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - why isnt this function returning any values? - Stack Overflow

programmeradmin4浏览0评论

i need to pass the variables to another function, but it isnt returning anything. NOTE: it doesnt return anything to the outside, but if i do a document.write inside the function it works perfectly..

<script type="text/javascript"> 
//funtion1
    if (navigator.geolocation){
        navigator.geolocation.getCurrentPosition(Location);
        }
    function Location(position){
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
    return (latitude,longitude);
        }


//function2
    function initialize() {
        document.write(latitude);
        document.write(longitude);}
</script>

i need to pass the variables to another function, but it isnt returning anything. NOTE: it doesnt return anything to the outside, but if i do a document.write inside the function it works perfectly..

<script type="text/javascript"> 
//funtion1
    if (navigator.geolocation){
        navigator.geolocation.getCurrentPosition(Location);
        }
    function Location(position){
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
    return (latitude,longitude);
        }


//function2
    function initialize() {
        document.write(latitude);
        document.write(longitude);}
</script>
Share Improve this question edited Dec 7, 2011 at 13:22 TCN asked Dec 7, 2011 at 12:15 TCNTCN 1,6511 gold badge29 silver badges46 bronze badges 2
  • i know im not calling it, but still it works!! it just isnt outputting the values – TCN Commented Dec 7, 2011 at 12:20
  • 2 I'd remend reading some basic JavaScript tutorials, rather than jumping in at the deep end and trying to do something without grasping the simplest concepts of the language. – Andy E Commented Dec 7, 2011 at 12:24
Add a ment  | 

5 Answers 5

Reset to default 9

You're misunderstanding a few concepts.

First, return (a, b) will evaluate to return b. The ma operator is almost never needed. If you want to return an array, use return [a, b].

Secondly, getCurrentPosition is asynchronous. It will execute Location if it has found the position. So at the time you call document.write, the position has not been fetched yet.

Lastly, you're not using the return value at all. If you want to use the return value of a function, use e.g. var result = func(...). However, in this case this doesn't make sense.

I don't exactly know what you're after, but anything that relies on the position should be put inside the callback (Location in this case). Only when the callback is called the poision is available. In this callback returning something is pointless since the return value is not used for getCurrentPosition.

Edit: Looking at your code, you want something like this instead of returning:

function Location(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;

    useValuesForSomethingElse(latitude, longitude); // call other function with position data
}

You can return only one value from the function. If you want to return more than one value, create a object and set the values in it to return. Otherwise use arrays to return more than one value

The .getCurrentPosition() function executes asynchronously and then calls Location() when done. In the meantime, whatever es after the call to getCurrentPosition() will execute but will have no results to work with. This also means whatever you return from Location() will not be used by any other code.

So you need to call your other function from within Location().

(Also, talking about function returns in general, you need to return a single value or an object/array. A ma-separated list of return values will in effect just return the last one in the list.)

Try to change your code into this:

<script type="text/javascript">
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var latitude = position.coords.latitude;
      var longitude = position.coords.longitude;
      document.write(latitude, longitude);
    });
  }
</script>

The function you pass into getCurrentPosition() will get executed when the position is retrieved. Only then you have access to the position and you are able to set latitude and longitude variables and write them into the document.

In your code getCurrentPosition() will be called, then immediately after that document.write(latitude,longitude); will get executed which is way before the latitude and longitude are available. You must move your document.write() into the Location().

Update 1 (based on ments):

If you don't want to do document.write() inside the callback function, but you want call another function, then you must call it from the callback function.

It will look like this:

<script type="text/javascript">
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var latitude = position.coords.latitude;
      var longitude = position.coords.longitude;
      myFunction(latitude, longitude);
    });
  }

  function myFunction(latitude, longitude) {
    // you can do here anything you wish
    document.write(latitude, longitude);
  }
</script>

Update 2 (based on ments and updated question):

A way how to handle loading the map based on the position is to to call initialize() from Location() with latitude and longitude as parameters. To make sure the code working with HTML elements is executed when page is loaded you can execute inside Location().

window.onload = intialize(latitude, longitude);

See this for a plete code.

navigator.geolocation.getCurrentPosition calls the passed function asynchronously as soon as the position is available (e.g. after the user accepted the prompt if the site may know the position).

You need to continue your code in the Location function - and do not return anything as it will not be used anywhere.

发布评论

评论列表(0)

  1. 暂无评论