脚本只工作第一次,而不是在IE8刷新后

Script only works first time, not after refresh in IE8

本文关键字:IE8 刷新 工作 第一次 脚本      更新时间:2023-09-26

嗨,我有这个代码在我的页脚,它在所有浏览器中工作得很好,但IE8,在IE8中,它只在直接页面加载上工作得很好,如果我进入域并点击进入,它就像一个魅力,但如果我点击刷新,我得到一个"对象预期"因为"谷歌。JSON.stringify(google.maps) == '[]'

你知道有什么问题吗?(如果我删除缓存并直接进入页面,它仍然可以正常工作。)

<script type='text/javascript' src='http://www.google.com/jsapi?ver=3.2.1'></script>
<script type="text/javascript"> 
 google.setOnLoadCallback(function(){
      jQuery( ".all-map" ).each(function( index ) {
        var obj = jQuery.parseJSON(jQuery(this).val());
        var allMap = new google.maps.Map(document.getElementById(obj.mapId), {
        zoom: obj.zoom,
        center: new google.maps.LatLng(obj.centerLat, obj.centerLong),
        mapTypeId: google.maps.MapTypeId.SATELLITE
        }
        );
        var locations = eval(obj.locations);
    var infowindow = new google.maps.InfoWindow();
    var marker, i;
    for (i = 0; i < locations.length; i++) {  
       marker = new google.maps.Marker({
       position: new google.maps.LatLng(locations[i][1], locations[i][2]),
       map: allMap
       }); 
     google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          jQuery('#'+obj.mapId).parent().siblings().eq(i).click();
          infowindow.setContent(locations[i][0]);
          infowindow.open(allMap, marker);
        }
      })(marker, i));
    }
  });
 });
</script> 

这是竞争条件。当信息已经在IE的缓存中时,google onload事件处理程序在DOM呈现之前运行(页面onload事件),并且地图对象不存在于DOM中,所以文档。getDocumentById无法找到它。您需要等待文档加载以及google script onload事件触发

我仍然不知道是什么错了,但我做了一些重组,当

$(document).ready(function() {
    google.load("maps", "3",  {
        callback: initialize , 
        other_params: "sensor=false"
    });
});

,现在它也可以在IE8中工作了,谢谢帮助!