Skip to content

用gzip压缩js和css,加速页面装载

2010 四月 17
tags: , , ,
by 荒野无灯

主流的Firefox ,Opera,IE,Chrome 最近版本都支持gzip压缩的页面。
可到 http://www.whatsmyip.org/http_compression/ 测试,结果匀为:

YES, your browser is requesting compressed content.

下面先上图,有图有真相:

压缩合并方法:
将要合并的js或css弄到一个文件中去,然后将该文件后缀修改为php,再在文件最顶端加上:
对于js

1
<?php if ( !( ( ini_get( 'zlib.output_compression' ) == 'On' || ini_get( 'zlib.output_compression_level' ) > 0 ) || ini_get( 'output_handler' ) == 'ob_gzhandler' )  && extension_loaded( 'zlib' ) ){   ob_start( 'ob_gzhandler' ); } header("Content-type: text/javascript"); ?>

对于CSS

1
<?php if ( !( ( ini_get( 'zlib.output_compression' ) == 'On' || ini_get( 'zlib.output_compression_level' ) > 0 ) || ini_get( 'output_handler' ) == 'ob_gzhandler' )  && extension_loaded( 'zlib' ) ){   ob_start( 'ob_gzhandler' ); } header("Content-type: text/css"); ?>

文件末尾加上:

1
<?php if(extension_loaded('zlib')) {ob_end_flush();} ?>

国内检测站点:
http://tool.chinaz.com/Gzips/

喜欢这篇文章吗?

请订阅本站 RSS feed填写您的邮件地址,订阅我们的精彩内容:,欢迎点击这里捐赠以支持荒野无灯转播到腾讯微博 转播到腾讯微博

作者:荒野无灯
出处:Hacklog【Hacklog】

声明: 本站遵循 署名-非商业性使用-相同方式共享 3.0 共享协议. 转载请注明转自Hacklog【荒野无灯weblog】

本文链接: http://ihacklog.com/?p=3632

5 Responses Post a comment
  1. 二月 15, 2011

    实际上在服务器端,如cpanel界面,可以设置,压缩整站传输,所以可以不用这个了

  2. 四月 17, 2010

    有时间试试

  3. 四月 17, 2010

    WordPress默认是不对CSS和js进行压缩的,但是对网页本身的传递进行压缩……
    无所谓啦。

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <q cite=""> <strong>

 :wink:  :-|  :-x  :twisted:  :)  8-O  :(  :roll:  :-P  :oops:  :-o  :mrgreen:  :lol:  :idea:  :-D  :evil:  :cry:  8)  :arrow:  :-?  :?:  :!:

Note: You may use basic HTML in your comments. Your email address will not be published.

Subscribe to this comment feed via RSS