最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Loading Google Maps API via AJAX, console error - Stack Overflow

programmeradmin5浏览0评论

I'm building a fully dynamic website using jquery/javascript, ajax and php.

When I click on a navigation link, the browser opens that page using ajax.

So basically all pages are loaded within the same index.php.

If I go to 'Location' tab, where I have a google map, it will load the Google Maps script dynamically (adding a script tag to the body).

<script type="text/javascript" src=".exp&amp;callback=initialize"></script>

This script is loaded automatically by the previous script

<script src=".js"></script>

When I leave the 'Location' page, I check if the scripts are present and remove them.

If I go back to 'Location' without refreshing the page, I thought the map would have a clean start, but I get this error in console:

You have included the Google Maps API multiple times on this page. This may cause unexpected errors.

Even though the scripts were previously removed, and the map and content changed to something else, I get that error.

Since I know I have only once instance of the map, should I just ignore it?

Or it really has some kind of reference to the old map, and removing the two scripts is just not enough.

Thank you for any information!

I'm building a fully dynamic website using jquery/javascript, ajax and php.

When I click on a navigation link, the browser opens that page using ajax.

So basically all pages are loaded within the same index.php.

If I go to 'Location' tab, where I have a google map, it will load the Google Maps script dynamically (adding a script tag to the body).

<script type="text/javascript" src="https://maps.googleapis./maps/api/js?v=3.exp&amp;callback=initialize"></script>

This script is loaded automatically by the previous script

<script src="https://maps.gstatic./maps-api-v3/api/js/19/1/intl/hr_ALL/main.js"></script>

When I leave the 'Location' page, I check if the scripts are present and remove them.

If I go back to 'Location' without refreshing the page, I thought the map would have a clean start, but I get this error in console:

You have included the Google Maps API multiple times on this page. This may cause unexpected errors.

Even though the scripts were previously removed, and the map and content changed to something else, I get that error.

Since I know I have only once instance of the map, should I just ignore it?

Or it really has some kind of reference to the old map, and removing the two scripts is just not enough.

Thank you for any information!

Share Improve this question asked Nov 30, 2014 at 17:39 BralemiliBralemili 1321 gold badge1 silver badge11 bronze badges 2
  • Why don't you just link gmaps library once and for all during first page load ? It's better than reloading it each time you need it and I guess it solves your problem. – Florian Motteau Commented Nov 30, 2014 at 18:21
  • You mean I add the gmaps script to index.php and use initialize() on a certain page? EDIT: Can this approach increase load time? – Bralemili Commented Nov 30, 2014 at 18:38
Add a ment  | 

1 Answer 1

Reset to default 10

Removing script-elements will not remove objects that have been created by these scripts( basically it will not have any effect).

Check if the maps-API already has been loaded:

<script>
jQuery(function(){
if(!window.google||!window.google.maps){
  var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'https://maps.googleapis./maps/api/js?v=3&' +
        'callback=initialize';
    document.body.appendChild(script);
}
else{
  initialize();
}});
</script>
发布评论

评论列表(0)

  1. 暂无评论