上线前用 JSMin 压缩你的 JavaScript 档案
< type="text/javascript">
在网页中动态载入的 JavaScript 对于页面显示的速度影响甚多,不得不注意!
尤其是很多人习惯把 JavaScript include 写在 <head> 区块中,这样一来最大的问题就是 Browser 必须要等到 JavaScript 全部读完后才会开始显示网页,如果你像信义房屋这样,读一个首页要下载 18 个 JavaScript 外部档且「没有大频宽」的话,首页就要等这 18 个 JavaScript 下载完才会开始显示网页,在这之前使用者就只会看到白白的一片画面!
所以我有两个建议:
1. 建议尽量将 JavaScript 摆在 </body> 之前
2. 建议将所有外部的 JavaScript 档案都先「瘦身」过!
至于瘦身的话,我是习惯用 JSMin 这个工具,在上线前跑一个自己写的批次档将所有 *.js 档压缩过再传到网站上,这样至少可以减少一些档案下载的时间。
你可到这个网站 ( http://www.crockford.com/javascript/jsmin.html ) 下载 jsmin.exe 档,使用方法是:
C:/> jsmin.exe < fulljslint.js > jslint.js
这样就可以将 fulljslint.js 压缩后储存到 jslint.js 档案了,但要切记不要输入与输出用相同的档名,这会把你的档案内容给清空!!
而我自己是写了一组 Batch 档 (*.bat) 做批次压缩的动作。
第一支:RunJSMin.bat ( 下列的档案请换成你们自己的 js 档 )
@echo off
SET JSDir=Website/js
CALL JsMinfy.bat %JSDir% common.js
CALL JsMinfy.bat %JSDir% debug.js
CALL JsMinfy.bat %JSDir% flashfix.js
CALL JsMinfy.bat %JSDir% jquery.cookie.js
CALL JsMinfy.bat %JSDir% png_transparent.js
CALL JsMinfy.bat %JSDir% swfobject.js
第二支: JsMinfy.bat
@REM 此档是给 RunJSMin.bat 呼叫用的
@REM 第一个参数是要压缩的 JavaScript 所在目录
@REM 第二个参数是要压缩的 JavaScript 档名
set DirName=%1
set FileName=%2
jsmin.exe < %DirName%/%FileName% > %DirName%/compressed.%FileName%
del /F %DirName%/%FileName%
ren %DirName%/compressed.%FileName% %FileName%
< type="text/javascript">
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ChaoYang0502/archive/2009/12/21/5048473.aspx