我试图检索分离函数的2个属性,我调试那里的值在函数结束之前,他们有一个值,但返回值alwas未定义我不知道为什么!javascript的返回值总是未定义
的js文件
function STAAPlanlat(){
alert ("the function");
if (navigator.geolocation) {
//we supposed to call the handlers of the expections
navigator.geolocation.watchPosition(function(position) {
alert("show position ");
// x.innerHTML="Latitude: " + position.coords.latitude +"<br />Longitude: " + position.coords.longitude;
var lat=position.coords.latitude;
var lan=position.coords.longitude;
//x.innnerHTML=out
alert(lat+"<"+lan);
return lan;
});
} else {
alert("error");
}
}
我得到了警报,局域网的值和纬度
但是当我呼吁分离文件时,它返回undefined返回值
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="STAAP1.2.js"> </script>
<script type="text/javascript">
function test(){
var out=STAAPlanlat();
document.getElementById("STAAPlanlat").value = "lan is"+out;
//document.writeln("lan is"+out);
}
</script>
</head>
<body>
<p id="STAAPlanlat">Test the division</p>
<button onclick="test()">STAAPlanlat()</button>
<button onClick="alertme()" >Alert</button>
</body>
</html>
非常感谢,现在的工作,很高兴你:) @jeschafe – 2012-07-11 21:50:23
这个答案帮助比其他任何我多,谢谢大家!我没有意识到从嵌入函数返回值,在这种情况下,ajax成功函数并没有将它返回到主函数以供在外部使用。 – 2018-02-07 11:08:17