码迷,mamicode.com
首页 > Web开发 > 详细

LABjs

时间:2014-10-14 16:34:08      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:http   io   os   ar   java   for   strong   sp   art   

LABjs (Loading And Blocking JavaScript) is an open-source (MIT license) project supported by Getify Solutions. The core purpose of LABjs is to be an all-purpose, on-demand JavaScript loader, capable of loading any JavaScript resource, from any location, into any page, at any time. Loading your scripts with LABjs reduces resource blocking during page-load, which is an easy and effective way to optimize your site‘s performance.

LABjs by default will load (and execute) all scripts in parallel as fast as the browser will allow. However, you can easily specify which scripts have execution order dependencies and LABjs will ensure proper execution order. This makes LABjs safe to use for virtually any JavaScript resource, whether you control/host it or not, and whether it is standalone or part of a larger dependency tree of resources.

Using LABjs will replace all that ugly "<script> tag soup" -- that is all the <script> tags that commonly appear in the <head> or end of the <body> of your HTML page. The API is expressive and chaining, to let you specify which scripts to load, and when to wait ("block"), if necessary, for execution before proceeding with further execution. The API also easily allows inline code execution coupling (think: inline <script> tags).

Loading LABjs dynamically

It‘s been asked several times before: "How would I load LABjs itself dynamically, to save on the blocking load of LAB.js (~2k gzip‘d)?" Since it gets asked fairly frequently, I threw together this little code snippet as a place to start.

Snippet to load LABjs itself dynamically

Some things to note:

  1. The size of the minified snippet is ~640b before gzip. This means you will be increasing the size of your HTML page by at least this much. That‘s not particularly a problem, but it is something to be aware of in terms of tradeoffs.
  2. My best guidance would probably be to use the snippet to load LAB.js dynamically on your first page load (landing page, etc), and then on all subsequent pages in your site, just load LAB.js normally with a <script> tag (since it should then load nearly instantly from cache).

 

Old and busted:

<script src="framework.js"></script>
<script src="plugin.framework.js"></script>
<script src="myplugin.framework.js"></script>
<script src="init.js"></script>

New hotness:

<script>
   $LAB
   .script("framework.js").wait()
   .script("plugin.framework.js")
   .script("myplugin.framework.js").wait()
   .script("init.js").wait();
</script>

LABjs

标签:http   io   os   ar   java   for   strong   sp   art   

原文地址:http://www.cnblogs.com/kaleidoscope/p/4024300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!