8. JSMin
The JavaScript Minifier
Douglas Crockford
www.crockford.com
2003-12-04
JSMin is a filter which removes comments and unnecessary whitespace from JavaScript files. It typically reduces filesize by half, resulting
in faster downloads. It also encourages a more expressive programming style because it eliminates the download cost of clean, literate
self-documentation.
What JSMin Does
JSMin is a filter that omits or modifies some characters. This does not change the behavior of the program that it is minifying. The result
may be harder to debug. It will definitely be harder to read.
JSMin first replaces carriage returns ('r') with linefeeds ('n'). It replaces all other control characters (including tab) with spaces. It
replaces comments in the // form with linefeeds. It replaces comments in the /* */ form with spaces. All runs of spaces are replaced with
a single space. All runs of linefeeds are replaced with a single linefeed.
It omits spaces except when a space is preceded and followed by a non-ASCII character or by an ASCII letter or digit, or by one of these
characters:
$ _
It is more conservative in omitting linefeeds, because linefeeds are sometimes treated as semicolons. A linefeed is not omitted if it
precedes a non-ASCII character or an ASCII letter or digit or one of these characters:
$ _ { [ ( + -
and if it follows a non-ASCII character or an ASCII letter or digit or one of these characters:
$ _ } ] ) + - " '
No other characters are omitted or modified.
23. jQuery
New Wave Javascript
jQuery is a new type of Javascript library. It is not a huge, bloated, framework
promising the best in AJAX - nor is just a set of needlessly complex
enhancements - jQuery is designed to change the way that you write
Javascript.
New: The jQuery Mailing List is now up - join and discuss!
New: The jQuery Blog has just been opened - subscribe now!
What is jQuery?
jQuery is a Javascript library that takes this motto to heart: Writing Javascript
code should be fun. jQuery acheives this goal by taking common, repetitive,
tasks, stripping out all the unnecessary markup, and leaving them short, smart
and understandable.
What does jQuery code look like? The quick and dirty:
The above code snippet looks for all paragraphs that have a class of 'surprise',
adds the class 'ohmy' to them, then slowly reveals them. Click the 'Run' button
to see it in action!
Quick Facts:
jQuery supports CSS 1-3 and basic XPath.
jQuery is about 10kb in size.
jQuery works in Internet Explorer, Firefox, Safari, and Opera.
Getting Started:
$("p.surprise").addClass("ohmy").show("slow");
Congratulations! You just ran a snippet of jQuery code - wasn't that
easy? There's lots of example code throughout the documentation, on
this site - be sure to give all the code a test run, to see what happens.