lazy load google maps api v3 jQuery callback

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 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)

Answers


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);  
});

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
    }});
});

Need Your Help

ADB Wireless Device Unauthorized

android wifi adb wireless

When connecting a device (like LG G2) with ADB Wireless, the device shows up as unauthorized. Why does this happen?

Inherit interfaces which share a method name

c++ visual-c++ gcc multiple-inheritance

There are two base classes have same function name. I want to inherit both of them, and over ride each method differently. How can I do that with separate declaration and definition (instead of