다음과 비슷한 javascript 코드가 있지만 foo() 내에서 'myVar'를 사용하려고하면 해당 값이 정의되지 않습니다. 또한 변수를 bar()에 전달하여 직접 할당했지만 참조로 전달하지는 않습니다. 그 차이를 만드는 경우 다음과 같은 HTML함수 내의 숨겨진 변수 할당
<input type="hidden" id="coords" name="coords"/>
실제 코드
function foo() {
bar();
var myVar = document.getElementById('coords').value;
// myVar should now be (40.7143528, -74.0059731)
alert(myVar); // returns blank
var request = {
location: myVar,
radius: 500,
types: [type] //previously defined
};
//Then send a search request to Google...
}
function bar() {
geocoder.geocode({'address':address}, function(results, status) {
document.getElementById('coords').value = results[0].geometry.location;
// Let's say the location is NYC (40.7143528, -74.0059731)
alert(document.getElementById('coords').value); //returns the correct value
// however, this pops up after the pop up in function foo()
});
}
구글지도 API를 사용하고 있습니다 : 나를 위해 잘
언제'foo'를 호출합니까? –
또한 디버그 경고를 배치하면 bar()가 호출되기 전에 'myVar'생성이 발생하는 것처럼 보입니다. –
foo()가 호출 됨 onClick (HTML의 숨겨진 변수 앞에) –