How to minify (not obfuscate) your JavaScript using PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    How to minify (not obfuscate) your JavaScript using PHP

    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
      [code=html]<script type="text/javascript" src="/scripts/myscript.js"></script>[/code]
      Instead of this, make a PHP function, something like this:
      [code=php]
      function send_script($sc ript)
      {
      echo "<script type=\"text/javascript\" src=\"/js.php?js=".$sc ript."\"></script>";
      //don't use the XML style closing tag (<script .... />) IE would not read it.
      }
      [/code]
      Keep this function in a saperate file.
    • In the root folder, save this PHP file (js.php)
      [code=php]
      <?php
      //define the folder name, so that it can be changed anytime.
      define("FOLDER_ NAME", "js_kl32lnfdn") ;

      //path for the original file
      $js_original = $_SERVER['DOCUMENT_ROOT']."/".FOLDER_NA ME."/".$_GET['js'];

      //header for the javascript file (will give a warning if not provided)
      header("content-type:text/javascript; charset:UTF-8");

      //alert error if file doesn't exisit
      if (!file_exists($ js_original))
      die('alert(\'Ja vaScript file error!\')');

      //include minificator class
      require('jsmin-1.1.0.php');

      //read and minify the content
      $minified = JSMin::minify(f ile_get_content s($js_original) );

      //header for caching
      header("Expires : ".gmdate("D , d M Y H:i:s", (time()+9000)) . " GMT");

      //year to write on copyright notice
      $year = getdate();
      $year = $year['year'];

      //send your copyright notice
      echo "/* Copyright ".$year." - www.your-website.com */";

      //send the minified version
      echo $minified;
      ?>
      [/code] 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=scrip t1.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
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Not sure why you wouldn't want a client to be able to read the comments?

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      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:[CODE=php]if (!isset($_GET['js']) || $_GET['js']=="")
      die('alert(\'Ja vaScript file error!\')');[/CODE]

      Comment

      Working...