我正在尝试确保Google地图是页面上最后加载的东西,并且不会对页面的性能产生负面影响。
将defer属性放在... sensor = false“之后时,地图不会显示。将defer属性与Google地图配合使用的最佳方法是什么?这可能吗?
<div id="map-canvas"></div>
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false" defer></script>
<script defer>
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(37.7599446, -122.4212681),
zoom: 12,
panControl: false,
disableDefaultUI: true,
scrollwheel: false,
mapTypeControl: false,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
panControlOptions: {
position: google.maps.ControlPosition.LEFT_CENTER
},
zoomControl: true,
zoomControlOptions: {
position: google.maps.ControlPosition.LEFT_CENTER
},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(37.7599446, -122.4212681),
map: map,
title: '805 Valencia St. San Francisco, CA'
});
var contentString = '<div id="map-content">' +
'<div id="siteNotice">' +
'</div>' +
'<h1 id="firstHeading" class="firstHeading">805 Valencia St.<br>San Francisco, CA</h1>' +
'<div id="bodyContent">' +
'' +
'<ul class="email-list"><li>[email protected]</li><li>[email protected]</li><li>[email protected]</li></ul>' +
'</div>' +
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentString,
maxWidth: 330
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
使用defer时,必须使用API的异步版本:
<script defer
src="http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize">
</script>
问题:
当您使用defer
脚本时,将在关闭文档时加载脚本-内容已加载。此外,将在内联递延脚本之后解析外部递延脚本。
这有两个与您的实现相关的副作用:
您不能使用API的同步版本,因为它使用了document.write
,在文档关闭后无法使用,
电话:
google.maps.event.addDomListener(window, 'load', initialize);
...出现在尚未加载Maps-API,尚未google
定义,初始化将永远不会执行的时候。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句