Shrinking Javascript the Better Way
Hello everyone!
There are many ways to shrink or obfuscate Javascript, simply search Google and you should find plenty of results. I found that many are programs that shrink it, then you upload the new code, which I was going to use until I found a better solution.
I found two that were recommended, jspacker and JSMin. After searching for jspacker and finding their site down, I went to JSMin. I found links at the bottom for PHP code that would do it on the spot, which made me happy because I figured I could figure out a way to do something tricky with it, which I did.
Through the use of .htaccess, JSMin and another file I created, I made the site I was working on automatically shrink all my Javascript files when called, but look completely normal in Dreamweaver.
Here’s the code:
Quick note, I keep all my Javascript files in a folder called “js” which is also where “minimize.php” is located. You can adjust this according to your setup.
Again, should adjust the include of the “jsmin.php” which can be downloaded here. Also, with the way I set it up, “minimize.php” must be in the same folder. You can adjust all of this however you like, but this was the best for my use.That’s it! Basically what’s happening is when the site calls your “example.js” file, it instead reads the file “minimize.php?JSPage=example”, which then minimizes and outputs the “example.js”.
-Kerry
Tags: htaccess, Javascript, JSMin, minimize, shrink


That is sweet. I was just talking with someone about compressing Javascript. That is really a beautiful way of doing it. Now you won’t have to keep a “source file” saved somewhere on your computer.
@Jaffer:
I’m pretty sure, since Rewrite occurs on the server side, browsers can’t tell their are being rewritten, so there should not be a problem with caching.
Somehow i missed the point. Probably lost in translation
Anyway … nice blog to visit.
cheers, Gaucho.
Here’s the link:
http://dean.edwards.name/packer/
Was working for me yesterday, it is down at the moment. Hmm … keep trying, it’ll be online soon enough
Also, there needs to be some sort of caching capabilities in minimize.php, right? Otherwise minifying code on every server hit will bring the server down.
Do you have a link to where I could get JSPacker? I looked but the main website was done, I don’t know if it was temporary but that’s why I went to JSMin, as I felt JSPacker was better.
I don’t know, I wouldn’t think it prevents them as that is, to the browser, that file. I have not tested it and could easily be wrong
.
Great idea! I’ve been using JSMin for my minification requirements as well. I also tried JSPacker, which has a much higher compression level, but it requires that the code adhere to some standards (doesn’t like missing semi colons etc). It’s going to take me a while to fix all such errors in my 35K lines of JS code, but you should give jspacker a try.
Just curious, does the rewrite rule prevent browsers from caching the JS file? Have you tested this?