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

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

问题描述

我懒加载 google maps 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)

推荐答案

因为回调必须是全局的,你可以通过在就绪处理程序中访问 window 来实现.

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

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

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