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

javascript - load google maps apis scripts when needed? jquery.getScript()? - Stack Overflow

programmeradmin6浏览0评论

I am using a google map with the full maps api on my website. The map is only visible and used once a specific link (e.g. Show Map) is clicked. Since this maps api is a rather large js file I want to load the api only when it's needed and not on page load like i'm doing it right now.

Right now I have this in my <head>:

<script src=";amp;v=2&amp;sensor=true&amp;key=ABQIAAAAnf9W..." type="text/javascript"></script>
    <script type="text/javascript" src=";></script>

I am using jQuery and I have a seperate script.js file where I store all my javascript.

The map I only shown when I click the following item:

$('#googleMap').live('click', function(e) {

Is it possible to use jquery getScript() to load the above scripts only when this link is clicked? Or at least load it "onLoad()" and not "onDomReady"?

I am using a google map with the full maps api on my website. The map is only visible and used once a specific link (e.g. Show Map) is clicked. Since this maps api is a rather large js file I want to load the api only when it's needed and not on page load like i'm doing it right now.

Right now I have this in my <head>:

<script src="http://maps.google./maps?file=api&amp;v=2&amp;sensor=true&amp;key=ABQIAAAAnf9W..." type="text/javascript"></script>
    <script type="text/javascript" src="http://maps.google./maps/api/js?sensor=false"></script>

I am using jQuery and I have a seperate script.js file where I store all my javascript.

The map I only shown when I click the following item:

$('#googleMap').live('click', function(e) {

Is it possible to use jquery getScript() to load the above scripts only when this link is clicked? Or at least load it "onLoad()" and not "onDomReady"?

Share Improve this question edited Dec 2, 2020 at 10:57 peterh 1 asked Apr 1, 2011 at 16:53 mattmatt 44.5k107 gold badges268 silver badges402 bronze badges 1
  • Yes you can do this, could you post a brief example of your maps activation code? – Josiah Ruddell Commented Apr 1, 2011 at 16:59
Add a ment  | 

1 Answer 1

Reset to default 10

First of all, you don't need to include both of those scripts. You only need:

<script src="http://maps.google./maps/api/js?sensor=false" type="text/javascript"></script>

Secondly, you can use the google loader (see the "Dynamic Loading" section) to fire up maps when the user requests it, like this:

$("#foo").click(function() {
    google.load("maps", "3", {other_params:'sensor=false', callback: function(){
        alert('maps API loaded!');
        var map; 
        // do stuff with your map
    });
});
发布评论

评论列表(0)

  1. 暂无评论