Javascript compression PHP class

Published Monday, January 21, 2008 in Javascript, PHP

In addition to my CSS compressor and constants parser I've also written a very simple Javascript compressor class that uses Nicolas Martin's PHP port of Dean Edward's Javascript packer.

It works in exactly the same way as the CSS-compressor. Create a new instance, pass the directory(ies) you want to look for .js-files in and then call the pack-method.

$jsc = new JSCompressor('js/');
echo $jsc->pack();

Reducing the number of HTTP requests the browser needs to make is essential for performance. Together with full packing of all the code, file-size is often reduced by up to 70% and the number of HTTP requests always down to 1.

Download the demo and have fun!

Rate It

Bookmark It

  • Digg
  • Furl
  • Google
  • Technorati
  • Ma.gnolia
  • BlinkList
  • Blogmarks
  • Rojo
  • StumbleUpon


5 comments so far, why don't you post one too?

Dirk Niemeier

Saturday, March 29, 2008 | View all comments by Dirk Niemeier

Nice, I integrated this CSS and JS Compressor on my website to compress all files with one click. All files together have a filesize of 66 KB and the compressor reduces this size to 30 KB. It safes more than 50% on my site und it is very easy to use. It's just a pity that the JS compressor does not work with my AJAX files, AJAX requests won't work anymore. I have to find out the reason, perhaps I can solve the problem when I change some settings in the compression script.

Dirk Niemeier

Saturday, March 29, 2008 | View all comments by Dirk Niemeier

Okay, the problem was a missing ;. Up to now this error has not occured because the affected code passage is called not very often. Now the uncompressed code has a size of 120 KB and the compressed code has 55 KB - great!


Saturday, March 29, 2008 | View all comments by Andreas

@Dirk - Before compressing your JS you should run it through JSLint. JSLint checks for missing semicolons and such.

Also, all my JSCompressor does is fetch all the code in a directory, Dean Edwards JS-packer takes care of the rest.

Cheers for the comments.


Monday, May 04, 2009 | View all comments by marco

I had also built my own JS compressor for my AJAX-run site. However, there are some issues on the compressed JS file, most especially when the file is compressed as to just remove unnecessary spaces, comments and putting the code together in one single line. To make my compressed JS file error free I had to explicitly put the ; (semicolon) statement terminator on each line, even on function declarations which use var (like var foo=function(){...};).


Tuesday, May 05, 2009 | View all comments by Andreas

You should run your code through JSLint before compressing it to make sure it's error-free.

Comments closed

Comments are closed

Post It

From June 02 to April 23

  1. Mogrify is what you're looking for if you want to convert multiple images to multiple other images in ImageMagick
  2. Tommorrow, finally, the inFamous demo will be friggin availble on PSN!! Suweeeeeeeeet
  3. Fuck canvas is cool, I've started playing around with old 3D-shit again :)

February 2016

1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29

Recent Comments

  1. Vlad on A follow-up to "An alternative to div overlays":
    I'll try this.....
  2. Steve on jQuery Live Ajax Search Plug-in:
    This is a great script but I've got...
  3. Frank on jQuery Live Ajax Search Plug-in:
    Hey, sounds like a great plugin! I ...
  4. Andreas on jQuery Drag to Select Plug-in:
    @Paul - I didn't take into account ...

Style Switcher

The style switcher allows you to change the look and feel of
Only CSS and JavaScript are changed. The XHTML stays the same.

For more information about the styles, check the styles page.


Random Quote

- The Dean

Random Images

Answer This!

Do you find the "scroll-pagination" annoying? (If you don't know what it is, scroll to the bottom of the first-page)

Blog Roll