Julian Klewes

Compressing moo.tools (javascript) even further







Times read: 239
Read today: 2
Read last: February 9, 2010

Compressing moo.tools (javascript) even further

popup tail

Moo.tools is neat, I love it and use it whenever I can, but one thing is still bugging me: its size.
Even though moo.tools can be downloaded compressed (using the great php5 version of dean edward’s packer), I personally find 18kb of javascript too much.

So I started researching a bit with Google and found out different way of compressing moo.tools (or actually whatever javascript you have) even further:

The first method, which I am using works like this:
+ You rename your javascript file (moo.tools) from the original filename.js to filename.js.php
+ Open the file with your favourite editor and add the following line just at the very TOP of the code:



+ Next, scroll down to the end of the file in your editor and add the following to the bottom:



+ Save your file and upload it.

Now add it to your website using the old HTML code, but keep in mind to add the .php-file extension.
It might look like this:



I was able to reduce the whooping 18kb moo.tools down to 9kb this way. Well, actually I did not do anything, but the server and its php and zlib.

Other methods:
At the top of Javascript:



.htaccess method
Save a compressed javascript file as *.jgz, and write .htaccess like this.


RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ".*Safari.*" [OR]
RewriteCond %{HTTP:Accept-Encoding} !gzip
RewriteRule (.*)\.jgz$ $1\.js [L]

AddType "text/javascript;charset=UTF-8" .jgz
AddEncoding gzip .jgz

Filesmatch .htaccess method:



ForceType text/javascript
Header set Content-Encoding: gzip

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !".*Safari.*"
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule (.*)\.js$ $1\.js.gz [L]
ForceType text/javascript

Sources / Ideas:
1. paulstamatiou.com
2. randomnetworks.com


People asked me to put up a donate button or something similar, so here it is: if you care to buy me a beer or Red Bull (I drink the Dutch equivalent called Spam Energy), feel free to donate, anything is appreciated!



h4x3d.com does not host any illegal content. Links/Files referred to are for educational purposes only.

Interact - Leave a comment

Engage in the conversion and leave a comment.
I would much love to hear from you. Please note however that comments are moderated by default. Advertising and rant is welcomed, yet often discarded...
See left/right for additional information













comment? jump to the form



archives | sitemap
contact | tos

Top

lorem

solarVPS.com - reliale enterprise hosting
you know the game, get in touch with me
3