See also: JavaScript
There are two aspects to including JavaScript in the output of a page. One part is including the libraries, the other is embedding javascript within the page to call those libraries. Internally, Tiki manages both of those through headerlib. That library also handles the CSS in a similar way.
As recommended to improve page loading speed, Tiki includes all JavaScript at the bottom of the page. Attempting to output script tags manually at random places in the output will cause errors. Including blocks of JavaScript should be done through the templates using the jq smarty plugin. Within that plugin, if dynamic elements are required, double curly brackets must be used to trigger smarty.
... {jq} var foo = {{$some_data|json_encode}}; $('#element').some_plugin(foo); {/jq} ...
The code will be called from within a jQuery .ready() event.
The javascript file inclusion must be made from PHP. When enabled, Tiki will minify the files into a larger JavaScript file. One file will be generated per combination of file included. For this reason, most of the file includes are located at the end of tiki-setup.php conditionally based on the features that require them. It may be counter-intuitive, but loading more upfront will improve performance on subsequent hits and the cost of the additional filesize it relatively small compared to the cost of an additional hit later on.
There is an exception however. If a javascript file is only used infrequently or by a small subset of users, it may be better to include it only when required. In this case, a special flag must be sent to indicate the file should not be part of the minified whole.
// Normal inclusion $headerlib->add_jsfile('path/to/file.js'); // Inclusion without packing $headerlib->add_jsfile('path/to/file.js', 'external');
As of tiki 8 (trunk as of writing) js files can be included in plugins, even though a plugin may be called several times during a wiki page's initiation. If called in the same controlled manner above, tiki is smart enough to both ensure that the files is only added , minified, and cached once if those features are turned on.