懒加载谷歌地图API

时间:2013-01-23 09:50:25

标签: javascript lazy-loading

我希望在显示主页后延迟加载所有第三方JS / CSS,因为当用户从主页导航到某个特定模块时会使用外部插件等。

到目前为止,我已经取得了正常的.js& .css外部库thanx到http://wonko.com/post/lazyload-200-released

然而,对于像http://maps.google.com/maps/api/js?sensor=true

这样的路径,这会失败

代码:

   LazyLoad.js('http://maps.google.com/maps/api/js?sensor=true', function () {
   alert('Your JS has been loaded');
   });

我认为解决方案是如何延迟加载网址?

2 个答案:

答案 0 :(得分:0)

我相信你想要的东西:

$.getScript('http://maps.google.com/maps/api/js?sensor=true');

使用jQuery。 API将提供回拨的详细信息。否则Google可能会有一些机制要求它从加载中出现。只是一个猜测。

答案 1 :(得分:-1)

找到解决方案: 检查网址:'http://maps.google.com/maps/api/js?sensor = true' 您会发现main.js正在被导入。传感器= true的简单getScript不会提供整个google对象,因此下次导入也是必需的。

  var t=setTimeout(function(){
    jQuery.getScript('http://maps.google.com/maps/api/js?sensor=true');
    jQuery.getScript('http://maps.gstatic.com/intl/en_us/mapfiles/api-3/10/20/main.js');
  },1000);