473,320 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,320 developers and data experts.

How to minify (not obfuscate) your JavaScript using PHP

hsriat
1,654 Expert 1GB
For a good programmer, providing good comments in the code is a must. In PHP, giving comments has no demerits. But what about JavaScript? Do you really want those comments to stay in the code and make it double in size, and of course disclose what you have done when the code reaches the client (browser)?

I am not sure how many of you would have faced this problem, but I faced this many times. I am not good at memorizing things, so I give lot of code comment in my JavaScript code. And never wanted that comments to reach the client. So I used to remove comments from each file by a PHP code and then upload that minified file to the server.

Then an idea came across my mind. And I never removed comments afterwards. May be some of use would already be using this thing, but it's new to me, so thought of posting here.

Here it is...
  • Instead of a regular name folder eg. js or scripts, in which you save your JavaScript files, save them in a random name folder. Something like js_kl32lnfdn.
  • We call our external JavaScript files as
    Expand|Select|Wrap|Line Numbers
    1. <script type="text/javascript" src="/scripts/myscript.js"></script>
    Instead of this, make a PHP function, something like this:
    Expand|Select|Wrap|Line Numbers
    1. function send_script($script)
    2. {
    3.     echo "<script type=\"text/javascript\" src=\"/js.php?js=".$script."\"></script>"; 
    4.     //don't use the XML style closing tag (<script ....  />) IE would not read it.
    5. }
    6.  
    Keep this function in a saperate file.
  • In the root folder, save this PHP file (js.php)
    Expand|Select|Wrap|Line Numbers
    1. <?php
    2. //define the folder name, so that it can be changed anytime.
    3. define("FOLDER_NAME", "js_kl32lnfdn");
    4.  
    5. //path for the original file
    6. $js_original = $_SERVER['DOCUMENT_ROOT']."/".FOLDER_NAME."/".$_GET['js'];
    7.  
    8. //header for the javascript file (will give a warning if not provided)
    9. header("content-type:text/javascript; charset:UTF-8");
    10.  
    11. //alert error if file doesn't exisit
    12. if (!file_exists($js_original))
    13. die('alert(\'JavaScript file error!\')');
    14.  
    15. //include minificator class
    16. require('jsmin-1.1.0.php');
    17.  
    18. //read and minify the content
    19. $minified = JSMin::minify(file_get_contents($js_original));
    20.  
    21. //header for caching
    22. header("Expires: ".gmdate("D, d M Y H:i:s", (time()+9000)) . " GMT");
    23.  
    24. //year to write on copyright notice
    25. $year = getdate();
    26. $year = $year['year'];
    27.  
    28. //send your copyright notice
    29. echo "/* Copyright ".$year." - www.your-website.com */";
    30.  
    31. //send the minified version
    32. echo $minified;
    33. ?>
    34.  
    The minificator PHP class is here: JSMin

What will happen is..
  • The JavaScript file will get minified by the js.php file, which in itself will use a minificator class (not made by me).
  • A file with path js_kl32lnfdn/script1.js will be called as js.php?js=script1.js. So one would never come to know the path to the original (commented) file.
  • For futher security, like if you only want the logged in user to be able to call the javascript or other things, you can add some verification code just in the starting of the js.php.


Hope you will like it.

Regards,
Harpreet
May 20 '08 #1
2 11567
Markus
6,050 Expert 4TB
Not sure why you wouldn't want a client to be able to read the comments?
Jun 24 '08 #2
hsriat
1,654 Expert 1GB
Just in case your comments are making your JS too bulky. For instance, for a complex DHTML page (like gmail, yahoomail), comments can make your JS to reach double in size than without comments. And of course it makes you code a bit uninterpretable.

__________________________________________

A little addition to the above js.php code, on the very first line:
Expand|Select|Wrap|Line Numbers
  1. if (!isset($_GET['js']) || $_GET['js']=="")
  2. die('alert(\'JavaScript file error!\')');
Jun 28 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: mthlv12 | last post by:
I am using the latest version of eclipse to develop a java application. Is there an obfuscate button in eclipse ?
20
by: twigster | last post by:
Hey everyone, I'm looking for a good way to obfuscate some Javascript code. Does anyone have a good experience or bad experience with a particular software? thanks
2
by: Laurent Bugnion [MVP] | last post by:
Hi group, A few years ago, I translated Douglas Crockford's JsMin from C to C#, because we wanted to use this functionality in our build process. JsMin is a code minimizer for JavaScript. It...
1
by: shapper | last post by:
Hello, Does anyone knows any C# Library to Combine and Minify CSS and Javascript files? I was able to find a few Rails and Java tools but no .NET tool. This would be really useful for ASP.NET...
50
by: Martin Rinehart | last post by:
How much javascript would you have when you decide that minifying is called for?
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.