延迟加载Google API无效

时间:2011-02-25 18:27:34

标签: javascript lazy-loading

这是交易。我已经尝试了一些延迟加载Google Maps API JavaScript的方法,每次使用时,使用我使用的任何Lazy Loader,FireFox都会“阻止”。懒惰加载Chrome就好了。

“块”的意思是HTML中的元素不显示,相反,浏览器会尝试建立连接以下载Maps JavaScript。

代码如下。将其放入HTML文件并在浏览器中打开:

<html>
<head>
    <script type="text/javascript" src="http://github.com/rgrove/lazyload/raw/master/lazyload.js"></script>
</head>
    <body>
        Open your console. Wait for "entering debugger..." message.<br />
        You can't see me in FireFox, can you?
        <script type="text/javascript">
            // If I remove the `sensor` from the query string key and value, 
            // Google rejects the request and alert() does occur
            LazyLoad.js("http://maps.google.com/maps/api/js?sensor=false", function() {
                console.log("entering debugger...");
            });
        </script>
    </body>
</html>

在Chrome中,我看到了“打开您的控制台...”页面内容,并在后台请求并下载了异步的JavaScript。

在FireFox中,“打开你的控制台...”内容永远不会出现,并且它会挂起(状态栏显示来自Google DNS的“阅读”),永远不会完成或显示页面内容。

5 个答案:

答案 0 :(得分:1)

您确定需要编写自己的装载机吗?如何使用以前测试过的实现:Lazy Load

答案 1 :(得分:1)

您是否尝试按需加载Google地图?要动态加载API,请传递一个回调参数,如Example

所示

答案 2 :(得分:0)

我认为你需要重命名你的LazyLoad脚本,因为it already exists. :)说到哪,它已经存在,那么为什么要再写呢?

如果您只是想看看它是如何完成的,source code可用。

答案 3 :(得分:0)

是的,为什么重新发明轮子。你似乎在使用jQuery。 试试这个插件: Load image only when it is viewable?

答案 4 :(得分:0)

找到解决方案: 检查网址:“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);

PS:类似问题线程Lazy loading google map api