1. Home
  2. Computing & Technology
  3. JavaScript

Problems With Using HTML Tidy on Pages Containing JavaScript

For people who are not very good at writing their own HTML or who want to clean up the output from a WYSIWYG (what you see is what you get) web editor so that the HTML actually validates thhen using the HTML Tidy program originally written by Dave Raggett is probably the best solution. This program will fix most of the errors that might be in the HTML for you and even does a reasonable job of removing most of the garbage that Microsoft Word insists on writing into files when you ask it to output in HTML format.

The program even adjusts the formatting of the HTML file in order to make the source code easier to read and make it easier to spot the cause of those errors that it is unable to fix itself (mainly due to there usually being several possible things that you might have meant the code to do and the program is unable to decide which of them you intended).

This reformatting is a problem though if your page contains JavaScript particularly if that JavaScript is inside of a <textarea> tag since the Tidy program will restructure the content of the textarea along with the rest of the web page which will convert any JavaScript that the textarea contains into complete garbage. It does this by inserting a whole lot of whitespace into various spots in the JavaScript where no whitespace is required and in some instances is not allowed.

The problem is that the program does not recognise that textarea content should not be reformatted when processing the page. This problem isn't just restricted to JavaScript as the same could happen with any text contained in a textarea that is required to be formatted a specific way.

There are two solutions to this problem, one alternative is to disable the reformatting completely when you run Tidy, the second is to go back and repair the page after Tidy has reformatted it.

To repair the page you first need to be aware of when you run HTML Tidy on a web page that contains a textarea. You then need to make sure that the update is not done in place where this applies so that you have access to the original version of the page in order to copy and paste the textarea content from the original into the reformatted copy of the page. Where you have both the original input to Tidy and the output it is a simple task to copy the correctly formatted content of the textarea from the original copy of the page and paste it in place of the garbage that Tidy places in the textarea in the formatted page.

Explore JavaScript
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.