I have described the savings achieved by this in a previous article, and it is a technique which quickly has become standard practice in recent years. Normally you would apply a minifier as a part of your build scripts, but for small sites and/or testing I have found it very useful to have a quick reference tool available.
Just paste your CSS or JS into the input-field below, and select the engine you would like to use. I normally use YUI Compressor, but my very informal tests actually gives a small lead to Microsoft AJAX Minifier, both with regards to speed and the attained compression.
Implementation with AJAX and cURL
Since the most popular minifiers are "real" programs, running them from a web page requires some trickery. After a bit of research, I found the following solutions for implementing my minifier tool:
- Executing the Java program directly from PHP
- Though you can execute Java programs from PHP (or EXE from ASP.NET), it usually requires security permissions which are normally not granted on standard web hosting1.
- Perform a POST to .NET code using AJAX2
- The minifiers I use are available in DLL versions, which can run under ASP.NET. However since my server is running Linux/Apache, this requires hosting on another server/domain, which means we can't readily use AJAX due to security constraints of the same origin policy.
Proxy the POST with PHP
To circumvent the XSS security restrictions mentioned above, we can proxy the call with PHP using
curl(). This enables us to POST to a local PHP script, which will then forward the request to the ASP.NET server and fetch the results server-side.
This is a common work-around, which is still secure if you observe proper input and host checking, and the posting will now work seamlessly with AJAX.