views:

2211

answers:

6

I want to cleanup some javascript files and reformat them with nice indents, etc, Any recommendations for utilities to do this under Windows

A: 

If you have Visual Studio you can open the Javascript document and press CTRL+K, CTRL+D to format the whole document.

DavGarcia
A: 

This page on about.com has a tool where you paste in your script and it cleans it up for you.

Diodeus
+2  A: 

Try the online JavaScript Tidy

Several attempts have been made to produce programs to reformat code, but these often fail to cope with situations such as '{', '}' or ';' characters inside strings, regular expressions, or comments, so virtually all of them end up with broken code. However, browsers already have their own parsers built in that deal with all of these situations. This script hooks into the browser's own parser, by wrapping the code in a function then using the native toString method to obtain the tidied code. As a result, it relies on the browser producing a tidied output, which there is no requirement for them to do. In fact, it relies on the browser decompiling to tidied code instead of just retaining the original, which is an expensive process in terms of performance, and leads to extra complexity when trying to make sure the decompiled code still works, so not all browsers choose to do it this way.

For best results, use Opera 9.2x- (not 9.5+), Safari or Konqueror on this page. It partially works in Firefox (nested functions are not tidied, meaning that most scripts you want to tidy still end up unreadable), and fails to reformat code in IE and iCab 3-. Since you can always keep a copy of Opera 9.2 on your computer (as well as whatever the current version is), I recommend you use Opera, and keep a copy of it around for use with JSTidy.

Ates Goral
great site, i will have to give that a try for similar problems
Jeff Winkworth
A: 

GNU indent tidies up C-style languages. Perhaps Javascript could be used as a subject as well.

strager
+6  A: 

Beautify Javascript is a good, simple option.

(Oops. Thought I grabbed the link for the app, not the home page.) ;)

Jonathan Lonowski
I use this one all the time.
epascarello
A: 

For offline use, this answer to a similar question explains how to set up the code Jonathan suggested to run from a command line with Rhino.

Matthew Crumley