![]() |
How to Minify JavaScript & CSS for Faster Page Loading
Minifying JavaScript and CSS reduces their file sizes considerably, which is accomplished by removing comments, unwanted newlines, space & tab characters, in more advanced cases long variable names are renamed into smaller ones and all the occurrences of the variable name is updated in the script - this is known as obfuscation - but, obfuscation leaves the script/CSS largely unreadable by a human. In the following example a JavaScript code is minified to remove unwanted, but remains quite readable.
The source (330 characters): Code: JavaScript
Code: JavaScript
The minified & ofbuscated code (104 characters): Code: JavaScript
Minifying Using YUI CompressorYUI Compressor is a tool provided for free by Yahoo!, we'll be using this to minify our JavaScript and CSS files. For YUI Compressor to work you'll need Java JRE installed on your system, you can know more about YUI Compressor at http://developer.yahoo.com/yui/compressor/. Here's how you can minify you CSS & JS files: Code:
java -jar /path/to/yuicompressor-2.4.7/build/yuicompressor-2.4.7.jar util_functions.js -o util_functions.min.jsIf you want to minify multiple files at once you can use this command: Code:
java -jar /path/to/yuicompressor-2.4.7/build/yuicompressor-2.4.7.jar -o '.css$:-min.css' *.cssMinifying Using JSMin PHP portjsmin-php is a PHP port of Douglas Crockford's JSMin (http://www.crockford.com/javascript/jsmin.html), get jsmin-php from http://code.google.com/p/jsmin-php/. Code: PHP
RelatedSpeed Up Your Pages |
| All times are GMT +5.5. The time now is 17:12. |