Google Ajax Libraries API 加速你的js脚本的加载

Google 花费了大量的时间来使网络应用变得更快,调用js脚本是我们经常要用到的功能之一,过多的脚本调用经常会使网页变慢。

Google似乎试图来解决这个问题,使大家在使用Javascript框架的时候能够更快速和简单,为此,Google最新提供了一个名为 Ajax Libraries API的东西。

Ajax Libraries API的理念很简单:把Javascript运行在Google的服务器上面, 通过Google快速分发服务器, 当有需要的时候进行Gzip压缩, 更重要的是,使用缓存机制来改善多人同时加载的情况.

使用AJAX Libraries API 有以下优势:

  • 开发者不需要考虑如何设置缓存机制,Google会为你准备好一切
  • 如果另外一个应用程序使用了同一个Javascript框架,那么客户端不再需要重复去获取脚本,因为其已经被缓存在用户的机器里面了。
  • 不需要考虑网络环境和流量限制
AJAX Libraries API 目前支持以下JS框架

Ajax Libraries API调用方式1:

例如:加载Prototype 1.6.0.2

<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js"></script>

Ajax Libraries API调用方式2:

 

 

<script src="http://www.google.com/jsapi"></script> 
<script> 
    
// Load jQuery 
    google.load("jquery""1"); 
    
// on page load complete, fire off a jQuery json-p query 
    
// against Google web search 
    google.setOnLoadCallback(function() { 
        $.getJSON(
"http://ajax.googleapis.com/ajax/services/search/web?q=google&;v=1.0&;callback=?"
    
// on search completion, process the results 
    function (data) { 
        
if (data.responseDate.results && 
            data.responseDate.results.length
>0) { 
            renderResults(data.responseDate.results); 
    } 
    }); 
}); 
</script>

 

posted @ 2009-06-02 20:11  _拖鞋_  阅读(627)  评论(0编辑  收藏  举报