Defer attribute doesn't work with Google Maps API?

南楼画角 提交于 2019-11-29 07:19:56

When you use defer you must use the asynchronous version of the API:

<script defer 
  src="http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize">
</script>

The issue:
when you use defer the script will be loaded when the document is closed- the content has been loaded. Furthermore external deffered scripts will be parsed after inline defferred scripts.

This has two side-effects related to your implementation:

  1. you can't use the synchronous version of the API, because it makes use of document.write , which can't be used after the document has been closed

  2. the call :

    google.maps.event.addDomListener(window, 'load', initialize); 
    

    ...comes at a point where the Maps-API isn't loaded yet, google is undefined, initialize will never be executed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!