我試圖檢索分離函數的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