473,387 Members | 1,582 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,387 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 11586
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.