延迟加载google maps api v3 jQuery回调 [英] lazy load google maps api v3 jQuery callback

查看:264
本文介绍了延迟加载google maps api v3 jQuery回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会延迟加载google地图api v3 javascript

I do lazy loading of the google maps api v3 javascript

文档说关于将一个回调参数放在url中的函数的名称,

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

所以我必须定义gmap_draw()函数。

So I must define the gmap_draw() function.

当我将此函数包含在domready块中时,它不可见。

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

此问题的任何解决方法? (除了将函数退出domready块)

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

推荐答案

因为回调必须是全局的,

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

这篇关于延迟加载google maps api v3 jQuery回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆