Posts

Showing posts from June, 2010

A Javascript best practice: one special semicolon

It is becoming common practice to concatenate javascript files. Such concatenation means that only one HTTP request is required to download the necessary javascript rather than several. This reduction in HTTP overhead reduces the time taken to download the particular web application. This concatenation can introduce an unforeseen problem: if, in one file, some code is not properly finished (i.e. with a semicolon), and the proceeding file begins with a closure
being opened, the interpreter will break.

Jquery plugins often begin with closures and so are susceptible to this problem. Beginning the file with a semicolon will prevent this kind of problem. Where a file begins with a closure, best practice should dictate that it begins with a semicolon. An example of this practice in action is source code of the jquery form plugin.



References:
http://groups.google.com/group/jquery-dev/browse_thread/thread/2ce10bc1bcf6a8d1?pli=1