I have a .html document with CSS and Javascript inside of document (no external files). Is there some online tool that would minify document and Javascript to some very small footprint? I saw many scripts kind of unreadable, where all variables and function names are replaced with one-letter names, etc. Please advice.

share|improve this question
1  
I think the keyword you are looking for here is "minify" – Jrod Jul 19 '12 at 20:37
1  
@Jrod: And "obfuscation" :) – mouad Jul 19 '12 at 20:37

8 Answers

up vote 3 down vote accepted

By using one of the many available minifiers.

There are even some that minify CSS.

share|improve this answer
But javascript minifiers would not change function names to one-letter names if are used by document, such as onload="pageLoad" - then pageLoad stays :-/ – Ωmega Jul 19 '12 at 20:38
1  
Some minifiers have the option of selecting whether or not to minify literals/function names/variable names. – David Titarenco Jul 19 '12 at 20:39
But so far I always found just non-complex minifiers. I would like to use some that take care of all parts: js, css, html – Ωmega Jul 19 '12 at 20:41
You're most likely going to have to use a combination of tools, I doubt there's any fire-and-forget toolkit out there. – David Titarenco Jul 19 '12 at 20:43
I downvoted your post because you are suggesting the op should be forced to use more than one tool. There are language aware tools that can minify code in various languages in a single pass. I posted below about a tool called Pretty Diff, for example. – austincheney Jul 21 '12 at 13:20
show 1 more comment

Try http://prettydiff.com/. It offers automatic language detection and can minify html, css, and javascript. This tool presumes that script tags without a mime type or with a javascript relevant mime type must contain either javascript or nothing. Likewise, style tags without a mime type or with the "text/css" mime type are presumed to contain CSS. The CSS and JavaScript are minified accordingly. The tool itself is written in javascript. There is no reason you should have to use more than one tool.

share|improve this answer
It does not rename for example classes, etc. - by doing that (if...) it would make result much shorter – Ωmega Jul 21 '12 at 13:20
Correct, renaming is not minification. That is obfuscation. The objective of minification is not to harm, with exception to comments, the ability to read the document after use of a beautification tool. A good minifier will not rename anything or alter the functionality of the javascript or css and leave the entire document's code on a single line with only the minimum amount of white space without impact to the content. – austincheney Jul 21 '12 at 13:22

Closure compiler has been correctly recommended for JS; but not many are aware of Google's Closure stylesheets. One of the closure stylesheets features is renaming, where

<style>
  .descriptive-parent-class-name .descriptive-element-class-name {color:red;}
</style>
<div class="descriptive-parent-class-name">
  <p class="descriptive-element-class-name">Lorem ipsum</p>
  <p class="descriptive-element-class-name">Lorem ipsum</p>
</div>

would become

<style>
  .a-b .a-c {color:red;}
</style>
<div class="a-b">
  <p class="a-c">Lorem ipsum</p>
  <p class="a-c">Lorem ipsum</p>
</div>

There'll be further minification too; and given the fact that OP indicates all resources are included into the html, this may end up saving quite a bit in traffic overhead.

NB: if you inspect any Google search results page, you'll see their class and ID names are almost never longer than 4 random characters

share|improve this answer

I am afraid you'll have to do it in two steps:

  1. manual search and replace of global objects in a text editor
  2. minifiers to finish the job on css and js

I had a similar question some time ago (about global objects and object keys). The answer I got was that no tool will make assumptions about the global context. In your example, they won't minify "pageLoad" because it might be used by another html or js fragment you didn't provide.

A workaround in your example would be to make the pageLoad function local and add the onload event dynamically in the script:

elt.onload=function(){pageLoad();};
share|improve this answer
+1 :: good point with dynamic onload - thanks – Ωmega Jul 21 '12 at 12:05

While JavaScript code can be compressed and decompressed within JavaScript (see other answers), it's actually hard to achieve the same result in CSS. There is currently no tool that will do more than remove unnecessary whitespace (see also Are there any agressive CSS Minification tools?), since identifier and class names cannot be changed without destroying the link between HTML and CSS.

Also, you can't remove anything from HTML markup except whitespace. Well, you could minify the class names and identifier, but there's currently no tool that will update those minified values in your CSS/JS files, so this would render your website ugly and broken. And even empty elements are often needed in order to get some CSS effects right (yes, I'm looking at you, IE6). This applies to JavaScript function names too.

TL;DR: Unfortunately there's no all-in-one-minify-everything tool. The closest thing is htmlcompressor.

share|improve this answer
What I would expect is to replace function names in html and then also in Javascript. As for now, for example, if you have onload=pageLoad, then this name of function will stay that size, but it would be nice to have it replace by some short (one-two letter) name. So there is some potentilal beyond whitespace compression... – Ωmega Jul 19 '12 at 21:13
Well, it's basically the same problem with CSS/HTML - you have to change the function names everywhere and watch out for collisions. – Zeta Jul 19 '12 at 21:15

I advise the HTML5 Boilerplate Build Script which can minify your JS and CSS.

Features:

  • Combines and minifies javascript (via yui compressor)
  • Inlines stylesheets specified using @import in your CSS
  • Combines and minifies CSS
  • Optimizes JPGs and PNGs (with jpegtran & optipng)
  • Removes development only code (any remaining console.log files, profiling, test suite)
  • Basic to aggressive html minification (via htmlcompressor)
  • Autogenerates a cache manifest file (and links from the html tag) when you enable a property in the project config file.
  • Revises the file names of your assets so that you can use heavy caching (1 year expires).
  • Upgrades the .htaccess to use heavier caching
  • Updates your HTML to reference these new hyper-optimized CSS + JS files
  • Updates your HTML to use the minified jQuery instead of the development version
  • Remove unneeded references from HTML (like a root folder favicon)
  • Runs your JavaScript through a code quality tool (optional)
  • Optionally precompile LESS formatted CSS
  • Optionally output JSDOC3documentation

Impact

share|improve this answer

Have never tried it but have heard real good reviews of Google's Closure compiler..You may want to try it out

share|improve this answer

Good javascript compressor is also Google's Closure Compiler and vice versa, to make compressed code a bit better readible you can use Javascript Beautifier. You can also have a look at phpEasyMin project.

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.