Click here to Skip to main content
Click here to Skip to main content
Go to top

OptimizeHTML: A tool to optimize HTML files

, 7 Jun 2000
Rate this:
Please Sign up or sign in to vote.
A simple script to reduce the size of a HTML source.

Sample Image - OptimizeHTML.gif

Introduction

Today's HTML editors are very easy to use, they support visual editing as well as syntax highlighting and auto-formatting of HTML source, for both the beginner and the advanced user's happiness. But things like auto-indenting make your files bigger and bigger, so that you may want to reduce the download time of your pages. Here it comes, this little script.

This little VBScript application strips unneeded characters from your HTML files. The result is not handy to edit again, so this script should be used as the last step before uploading. However, reloading the file into your favorite auto-formatting editor should bring it again to its previous state.

OptimizeHTML Usage

There are several ways to use this script:

  • You may double-click on it or execute from the Shell.

    The main dialog will appear asking for some files to optimize. You may specify either an absolute or a relative path. If the filename part of the path is a '*' (asterisk) character, it will process all the .htm and .html files in the given directory (current directory, if none is specified). It will not recurse subdirectories.

  • You may drop some files on it or give command line arguments.

    The program will process each file dropped (or from the command line).

If more than one file is to be processed, the script will ask for confirmation.

You may personalize the program's behavior and even the language. These are overridable options:

'be careful with this
Const REMOVE_COMMENTS = False
'removes some META tags used by editing applications
Const REMOVE_PROGID = False
'try with this option enabled first
Const MAKE_BACKUPS = True
REMOVE_COMMENTS removes the comments enclosed in the <!-- --> tag
REMOVE_PROGID removes <META NAME="GENERATOR" ... > and <META NAME="ProgId" ... > tags
MAKE_BACKUPS makes a backup copy of each file processed, appending a .bak to the filename

And these are the localizable strings:

Const INPUT_FILE = "HTML file to optimize:"
Const INPUT_TIP = "Tip: 'path\*' means all .htm .html files" & _
                  " in given path (default is current path)"
Const PROCEED_MSG = "Optimization of all .htm .html files in folder:"
Const PROCEED_ASK = "Proceed?"
Const PROCEED_CMD1 = "Optimization of "
Const PROCEED_CMD2 = " files from command line."
Const COMPLETE_MSG = "Optimization completed."
Const ERR_NOTFOUND = "File does not exist."

You may wish to create a shortcut to OptimizeHTML.vbs in your SendTo directory and it will optimize all the files you send to it.

More on HTML Validation and Optimization

If you want to know more about HTML and related technologies, this is a site you must visit: The World Wide Web Consortium.

You may also take a look at the W3C Open Source Software in that same page and try the good HTML Tidy utility by Dave Ragett. Here is an excerpt from his page:

"When editing HTML, it's easy to make mistakes. Wouldn't it be nice if there was a simple way to fix these mistakes automatically and tidy up sloppy editing into nicely laid out markup? Well now there is! Dave Raggett's HTML TIDY is a free utility for doing just that. It also works great on the atrociously hard to read markup generated by specialized HTML editors and conversion tools, and can help you identify where you need to pay further attention on making your pages more accessible to people with disabilities."

If you want to validate your HTML pages, you may use the HTML Validation Service, always from the W3C site.

Conclusion

I hope you will find this script useful for your HTML editing. Please, note that this script only strips some characters from the specified files, it can't validate them.

If you improve the code, please let me know and make it available to other people. For any comments, suggestions or critics, use the space below.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Paolo Messina
Software Developer RoboTech srl
Italy Italy
Paolo began programming at the age of 9 with a glorious 8086 and GW-BASIC, then he played a bit with C, ASM and Pascal. He tought himself MFC and Windows programming, to exploit his studies of C++. Always attracted by low-level programming and Assembly, he's beginning to appreciate the joys of templates and STL. At work he changed his mind about Java, discovered Eclipse IDE, and now think it's cool.
 
He lives in Follonica, Italy.
 
He has been abroad in the U.S. to work on his final thesis before graduating. For seven months he was playing with airplanes and automatic control at the Unversity of Illinois at Urbana-Champaign.
 
He graduated in Computer Science Engineering at the University of Pisa, Italy, in December 2003.
 
Currently working for an edutainment robotics company (www.robotechsrl.com).
Follow on   Google+

Comments and Discussions

 
QuestionOnly whitespace compression? PinmemberAnonymous29-Sep-01 22:24 
GeneralI need an HTML Formatting tool PinsussAlvaro Mendez11-Jul-00 8:40 
I'm looking for an HTML formatting tool -- basically the opposite of yours. I use Visual Interdev to visually create my HTML forms. Unfortunately when I switch to Source mode, the HTML that is generated is horribly formatted. Lines are not properly indented and many are broken up in weird places.
 
So if I could highlight the HTML, copy it to the clipboard, paste it into a tool like yours, press a button, and then paste it back to VI nicely formatted, that would be great!
 
Thanks,
Alvar
GeneralRe: I need an HTML Formatting tool PinsussPaolo Messina14-Jul-00 3:18 
GeneralRe: I need an HTML Formatting tool PinsussAnonymous12-Sep-03 5:31 
GeneralRe: I need an HTML Formatting tool PinsussTed Crow1-Oct-00 10:14 
GeneralBatch mode is a good idea PinsussStephane Rodriguez11-Jun-00 23:48 
GeneralRe: Batch mode is a good idea PinsussPaolo Messina12-Jun-00 7:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 8 Jun 2000
Article Copyright 2000 by Paolo Messina
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid