Minifying HTML

2020-06-02 08:14发布

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?

标签: html minify
11条回答
时光不老,我们不散
2楼-- · 2020-06-02 08:15

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

查看更多
在下西门庆
3楼-- · 2020-06-02 08:20

I've used this regexp for years, without any problems: s/>\s*</></g

In Python re.sub(r'>\s*<', '><', html)

Or in PHP preg_replace('/>\s*</', '><', $html);

This removed all whitespace between tags, but not anywhere, this is fairly safe (but not perfect, there are situations where this will break, but they're rare).

My main reason for doing this isn't speed/file size, but because the whitespace often introduces a, well, space. This would be okay, but when you start mucking about in your DOM with Javascript, spaces are often lost, creating (minor) layout differences.

Consider:

<div>
    <a>link1</a>
    <a>link2</a>
</div>

There's a space between the links, but now I do something like:

$('div').append('<a>link3</a>')

And there's no space ... I need to manually add the space in my JS, which is fairly ugly & error-prone IMHO.

查看更多
何必那么认真
4楼-- · 2020-06-02 08:23

I recently found a PHP based script that minify your sites HTML - Inline css - Inline javascript on the fly it is called as Dynamic website compressor

查看更多
Bombasti
5楼-- · 2020-06-02 08:29

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)

查看更多
家丑人穷心不美
6楼-- · 2020-06-02 08:33

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!

查看更多
戒情不戒烟
7楼-- · 2020-06-02 08:34

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

查看更多
登录 后发表回答