Fullscreen
[Show/Hide Right Column]

Close
noteNote
This page is to document "what Tiki should do". For feature documentation (what Tiki does), please see corresponding page on doc site

Including 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.

Sample javascript inclusion from 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.

File inclusion
// 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.


Page last modified on Saturday 10 September, 2011 23:41:52 UTC

Search Wishes (subject only) [toggle]

Categorize Including JavaScript

Keywords

The following is a list of keywords that should serve as hubs for navigation within the Tiki development and should correspond to documentation keywords.

Each feature in Tiki has a wiki page which regroups all the bugs, requests for enhancements, etc. It is somewhat a form of wiki-based project management. You can also express your interest in a feature by adding it to your profile. You can also try out the Dynamic filter.