不能在函数外使用全局javascript变量

时间:2015-10-14 14:13:16

标签: javascript variables scope

我正在使用此功能与谷歌地图

var currgeocoder;
navigator.geolocation.getCurrentPosition(function (position, html5Error) {
    geo_loc = processGeolocationResult(position);
    currLatLong = geo_loc.split(",");
    initializeCurrent(currLatLong[0], currLatLong[1]);
});

function processGeolocationResult(position) {
    html5Lat = position.coords.latitude; //Get latitude
    html5Lon = position.coords.longitude; //Get longitude
    html5TimeStamp = position.timestamp; //Get timestamp
    html5Accuracy = position.coords.accuracy; //Get accuracy in meters
    return html5Lat + ", " + html5Lon;
}
alert(html5Lat);

如您所见,我没有使用var将变量设为全局变量,但alert()不起作用,如果我将alert()置于函数内部,则效果很好。那么如何从另一个函数访问html5lathtml5lon全局?

0 个答案:

没有答案
相关问题