2013-11-21 48 views
0
<script 
     src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> 
    <script> 
     var geocoder = new google.maps.Geocoder();  

    <%for (String st : t.keySet()) {%> 
     printGeocode( <%=t.get(st)[1]%>  , <%=t.get(st)[0]%>  );  
    <%}%> 
     function printGeocode(x, y) { 
      document.write("Test Check1<br>"); // work fine 
      alert("test Check1"); // work fine 
      var latlng = new google.maps.LatLng(x, y); 
      geocoder.geocode({ 
       'latLng' : latlng 
      }, function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        if (results[1]) {      
         document.write(results[1].formatted_address+"<br>"); // didn't work 
         alert(results[1].formatted_address); // Work Fine 
        } else { 
         alert('No results found'); 
        } 
       } else { 
        alert('Geocoder failed due to: ' + status); 
       } 
      }); 
     } 
    </script> 

這是我的源的Javascript API GoogleMap的文件撰寫()不工作

警報(字符串)是做工精細

但文件撰寫就是不工作

什麼是第一 「document.write("Test Check1<br>"); // work fine

document.write(results[1].formatted_address+"<br>");

01之間不同

出現警報的地址爲什麼

document.write(results[1].formatted_address+"<br>"); // didn't work 

沒有出現在網頁上?

+0

使用document.write不好。更好的操作DOM,像'document.getElementById('id')。innerHTML = results [1] .formatted_address +「
」' – Praveen

回答

1

document.write()只有當瀏覽器正在構建DOM時,API纔是安全的。在地圖API的回調中使用它意味着在它運行時,DOM已經完成。此時,document.write()的呼叫意味着應該清除DOM並準備新的DOM。

如果您需要添加內容,請使用DOM操作API和/或innerHTML

+0

thanks document.write()is evil :( – user2637015