简体   繁体   中英

ReferenceError: google is not defined (error after ajax call)

I got this error message for the google map after using ajax call.

ReferenceError: google is not defined 

What I'm doing is I put the link " <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> " inside the file(showMap.php) being called by ajax.

However, if i refresh the page, the google map work fine. And if I put the link mentioned outside the file being called by ajax, for example(mainPage.php) , the google maps work fine also. Any idea on how to make the link working inside the showMap.php file?

What's happening is your code is executing before the Google library is loaded.

In order to execute the code for the map, reference it as a callback function on the Google Maps API URL.

For example: <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&callback=init">/script>

  <script>
function init() {
//doStuffWithMapsAp
};
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM