tags:

views:

1002

answers:

9
+6  Q: 

Minifying HTML

I've googled around but can't find any HTML minifacation scripts.

It occoured to me that maybe there's nothing more to HTML minifacation than removing all unneeded whitespace.

Am I missing something or has my Google Fu been lost?

+1  A: 

You can find some good references here to things like HTML tidy and others.

If you don't want to use one of those options, Prototype has a means to clean the whitespace in the DOM. You could do that on your own and copy it via 'View Generated Source' in the Firefox extension Web Developer Toolbar. Then you can replace the original html with prototype's fix. Sorry for not making that apparent nickf.

(I recommend the first link)

geowa4
Prototype is a client-side script, so the HTML will already be sent unminified, thus defeating the purpose...
nickf
+1  A: 

Yes I guess it's pretty much removing whitespace and comments. You cannot replace identifiers with shorter ones like in javascript, since chances are that CSS classes or javascript will depend on those identifiers.

Also, you should be careful when removing whitespace and make sure that there is always at least whitespace character left, otherwise allyourtextwilllooklikethis.

DrJokepu
And even comments aren't safe to remove. You'd get rid of IE's conditional comments too for one. And on occasion I've used comments to hide whitespace that was causing bugs in IE.
mercator
mercator: Very true
DrJokepu
+1  A: 

There's a pretty lengthy discussion on this Wordpress blog about this topic. You can find a very lengthy proposed solution using PHP and HTML Tidy there.

cowgod
+1  A: 

Outside of HTML Tidy/removing white space as the other answers mentioned, there isn't much.

This is more of a manual task pulling out style attributes into CSS (hopefully you're not using FONT tags, etc.), using fewer tags and attributes where possible (like not embedding <strong> tags in an element but using CSS to make the whole element font-weight: bold, unless of course it makes semantic sense to use >strong<), etc.

Mufasa
+4  A: 

Sometimes, depending on the enclosing tags and/or on the CSS, whitespace may be significant.

ChrisW
+18  A: 

You have to be careful when removing stuff from HTML as it's a fragile language. Depending on how your pages are coded some of that whitespace might be more significant; also if you have CSS styles such as white-space: pre then you may need to keep the whitespace. Plus there are numerous browser bugs, etc, and basically every character in an HTML file might be there to satisfy some requirement or appease some browser.

In my opinion your best bet is to design the pages well with CSS techniques (I was recently able to take an important page on the site I work for and reduce it's size by 50% just by recoding it using CSS instead of tables and nested style="..." attributes). Then, use GZip to reduce the size of your pages for browsers that understand gzip. This will save bandwidth while preserving the structure of the html.

Mr. Shiny and New
+1 for using Gzip.
nickf
+1 for tables are NOT FOR ANYTHING OTHER THAN TABULAR DATA.
Sneakyness
A: 

Couldn't JavaScript be used as a decompresser for a compressed HTML string, for instance have a DEV build for the uncompressed format, run a 'publish' script to compress the DEV build to production and attach a JavaScript to the HTML source (with the whitespace and such removed as before)?

The bandwidth would be reduced on the server, but the downside is there is a lot more client strain for decompressing the string to HTML. Also JavaScript would need to be enabled and be able to parse the decompressed string to HTML.

I am not saying its a definite solution, but something that might work - it all depends on if your looking in regards to bandwidth without the users JavaScript permissions/systems spec, or such.

Otherwise look for obfuscation scripts, a simple google search produced http://tinyurl.com/phpob - dependent on what your looking for there should be a software package available.

If I am on the wrong lines, please shout and I will see what else I can do.

Good Luck!

Robinb
A: 

You can test a html minifier, markupmin.js, at http://mailmarkup.org/prettydiff/prettydiff.html

It also minifies the contents of style and script tags.

+1  A: 

I haven’t tried it yet, but htmlcompressor is an HTML minifier, if you fancy giving one a try.

Paul D. Waite