Edgewall Software

Version 4 (modified by techtonik <techtonik@…>, 15 years ago) ( diff )

add comments from discussion http://groups.google.com/group/trac-dev/browse_thread/thread/bd2ad5f23b6a988

JavaScript for Trac and plugin development

JavaScript is used in Trac to add dynamics to web interface elements on the browser side:

jQuery

Trac makes heavy use of jQuery library to ease development. Access to this library contents is usually provided through the main function named $. $ as a name used for brevity and clarity. Unfortunately, other libraries may use it too and this may cause conflicts. $ in fact is just a shortcut for jQuery namespace and it is possible to use full name instead. But to avoid conflicts with other libraries $ shortcut should is turned off by switching jQuery into non-conflict mode with jQuery.noConflict() call. This is well explained in http://docs.jquery.com/Using_jQuery_with_Other_Libraries

However, you will see many blocks in Trac that use $ for jQuery. They do it in their local scope defined by nameless function (or closure):

(function($) { /* some code that uses $ */ })(jQuery)

There is a good description of closures and (function(){})() construct at http://ajaxian.com/archives/secrets-of-the-javascript-ninja-a-sneak-peak-for-ajaxians

$(document).ready()

To execute and modify DOM tree JavaScript function should usually wait until a page fully loads. With jQuery it looks like:

$(document).ready(function_name); 

In non-conflicting mode, code that executed in Trac on page startup is enveloped in closure and looks like:

jQuery(document).ready(function($) { ... }); 
Note: See TracWiki for help on using the wiki.