开发者

lazy load google maps api v3 jQuery callback

开发者 https://www.devze.com 2023-01-23 20:06 出处:网络
I do lazy loading of the google maps api v3 javascript The documentation says about putting as a callback parameter in the url the name of the function, which will be executed, when the script has开发

I do lazy loading of the google maps api v3 javascript

The documentation says about putting as a callback parameter in the url the name of the function, which will be executed, when the script has开发者_JAVA百科 loaded.

 $(document).ready(function(){
   var s = document.createElement("script");
   s.type = "text/javascript";
   s.src  = "http://maps.google.com/maps/api/js?v=3&sensor=true&callback=gmap_draw";
   $("head").append(s);
 });

So I must define the gmap_draw() function.

When I enclose this function in the domready block, it is not visible.

Any workarounds of this issue ? (except putting the function out of the domready block)


Another option is to use Google Loader:

$.getScript('https://www.google.com/jsapi', function()
{
    google.load('maps', '3', { other_params: 'sensor=false', callback: function()
    {
        // Callback code here
    }});
});


Because the callback must be global, you could make one by accessing window from within the ready handler.

$(document).ready(function(){
   var s = document.createElement("script");
   s.type = "text/javascript";
   s.src  = "http://maps.google.com/maps/api/js?v=3&sensor=true&callback=gmap_draw";
   window.gmap_draw = function(){
       alert ("Callback code here");
   };
   $("head").append(s);  
});
0

精彩评论

暂无评论...
验证码 换一张
取 消