0

Since I've heard people whining how PHP can't handle it, how about minifying JavaScript with JavaScript itself? I want something that I can just copy and paste and use right away--my JS file is about 17KB and I know I can reduce it to about 15KB easily.

And I want to be able to do it right on the server. I don't want to have to go to a website such as jscompress.com every time I make changes to my script.

Again, I would like something like this:

<script type="text/javascript" src="scripts.php?file_to_minify=scripts_template.js"></script>

And then I would just let JS minify it on the PHP file.

Now, I do have second thoughts...Would this even work???

Your help is appreciated. Thanks!

8
  • 2
    I'm confused.. you can do what you propose in your code sample.. but that would be using PHP to minify the script. However didn't you say you don't want to use PHP? Commented Jul 8, 2012 at 3:23
  • Say I have a function in JS on the scripts.php page called "minify_js". What I would do is simply this minify_js(<? echo(file_get_contents($_GET['file_to_minify'])); ?>); I'm not sure if this would work though, which is why I'm asking if it would. Commented Jul 8, 2012 at 3:26
  • 2
    @RickyAYoder - What could you possibly gain by sending unminified code to the browser, and then minifying it on the client side??? Commented Jul 8, 2012 at 3:27
  • Reducing 17K to 15K is most likely ... pointless. In any case, this is what HTTP deflate compression (and caching) is for. Commented Jul 8, 2012 at 3:27
  • You should just look for a tool that watches for when you make changes to your JS source directory and runs a minifier, writing the output to a minified directory. I know they exist, although I haven't needed them myself as such is part of a separate build process for me. Commented Jul 8, 2012 at 3:33

1 Answer 1

1

Personally, I would look to have a local version of my site, with all source/configuration files under version control. You could then have a release script that would prepare your files for upload, including minifying your javascript using a command line tool.

This ensures that you don't lose anything if you make a mistake, also it makes build of your site repeatable. This should be requirement for a production site, and is useful for a personal/hobby site (no-one likes losing changes/breaking things with no way back).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.