Compress (minify JS) is one of most important missions if you need to optimize your web performance. And using JavaScript compressors is the easiest way to do this task, I recently present some popular minify JS compressors in Performances of JavaScript Compressors.
However, this solution has a big advantage you should know: we have to compile our JavaScript source codes/files before uploading them to live server if we change the source code. In order to overcome this shortcoming, we can use an alternation: compressing JavaScript source codes/files automatically on our server then give back the results as requested.
That's subject of this JavaScript article tutorial: guides you how to compress JavaScript source codes/files on Apache.
- Demo
- Enlarge
- Reload
- New window
Free iPage Web Hosting for First Year NOW
If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?
Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.
One of the most recommended (and easiest to implement) techniques for web pages optimization is compressing the CSS and JavaScript files used.
While this is not a particularly difficult thing to do manually (for example, using the YUI compressor found here), when releasing a new version on the live server, it would be much easier to use an automated method that allows you to write the JavaScript in your own development style (using comments and a more loose formatting), and serve it directly compressed to the browser, on request.
Well... wonder no more! We're going to present an easy and bullet-proof method of doing just that, on an Apache web server. For this purpose, we're going to use Dean Edwards JavaScript's Packer class, more precisely its PHP version, "translated" by Nicolas Martin.
You're going to have to add it to your JavaScript folder (assuming you have such a folder in your project, otherwise... add it in your common folder, or... wherever you want). In the same folder, you have to add an index.php file that will load this PHP class and use it to dynamically compress your JS files.
The content of the JS folder's index.php file should be something like
if(!empty($_GET['script']) && is_file($_GET['script'] . '.js')){
require_once '/relative/path/to/JavaScriptPacker.class.php';$script = file_get_contents($_GET['script'] . '.js');
$packer = new JavaScriptPacker($script, 62, true, false); // Default configuration
$script = $packer->pack();header('content-type:application/javascript');
echo $script;
}
This, as you may notice, will receive a script parameter (type GET) and, if it will fit a file in your JS folder named as its value (plus the .js extension), will compress and output it.
In order to automatically call the index.php page with the JS file's name (without the .js extension), you need some .htaccess commands. These are:
Options All -Indexes # Disabled direct access to your JS folder's content
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /path/to/your/JavaScript/folder/RewriteRule ^(.*)\.js$ ?script=$1 [L] # Here you instruct the server to pass any JS file called directly, to index.php as its script parameter
</IfModule>
As you may see, you must have the mod_rewrite module installed and active for this method to work.
If you have followed and understood all of the above... you now should access ONLY compressed JS files when calling them directly from your address bar (for example, try accessing http://your.domain.name/path/to/your/js/file.js - you should see something beginning with
eval(function(p,a,c,k,e,d){ ...
So... that's it! You now serve compressed (and somewhat "encoded") JavaScript files, thus minimizing the traffic and the time needed for the browsers to load your scripts.
One way of seeing the effects of this change is using either Page Speed or YSlow Firebug plug-ins. Try testing your web page without this compression method, and then, with it enabled. Depending on the size of your original script(s), you should be able to increase your ratings between 2-3 and up to 10 points.
Of course, you could go one step further, and try concatenating all of your scripts (in case you use more than one - and this is usually the case), and THEN compress it - you should gain up to 15 points in the performance rankings. But... I'll let you do that yourselves.
- Sent (0)
- New
Generate your business videos by AI with voice or just text
chatGPTaz.com
Talk to ChatGPT by your mother language
AppAIVideo
Your first FREE AI Video App
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Temu Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program
Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing
Dall-E-OpenAI.com
Generate creative images automatically with AI
chatGPT4.win
Talk to ChatGPT by your mother language
First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language
Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools
GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project
iPhoneKer.com
Save up to 630$ when buy new iPhone 16
Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k