$
where possible:
$(function() { ... });
instead of
$(document).ready(function() { ... });
$
If you want to use jQuery’s $
variable, wrap your code like this:
(function($) { // code using $ variable })(jQuery);
If you want to use global variables within the scope of your code, wrap your code like this:
(function() { var foo = 'bar'; // yay, it's almost like I'm global })();
If you want to use global variables in the global scope, put the variable in the foswiki
namespace:
foswiki.foo = 'bar';
Mind the predefined global variables. See next section.
The jquery.foswiki
plugin will initialize the global foswiki
object with a set of variables
that are created by reading meta
tags in the HTML header. These have the format
<meta name="foswiki.foo.bar.baz" content="string/boolean/object/function" />
foswiki
object content
attribute.
There is a set of predefined variables that can be used in your javascript code via the foswiki
namespace:
Name | Content |
---|---|
foswiki.web | %WEB% |
foswiki.topic | %TOPIC%" |
foswiki.scriptUrl | %SCRIPTURL% |
foswiki.scriptUrlPath | %SCRIPTURLPATH% |
foswiki.pubUrl | %PUBURL% |
foswiki.pubUrlPath | %PUBURLPATH% |
foswiki.systemWebName | %SYSTEMWEB% |
foswiki.usersWebName | %USERSWEB% |
foswiki.wikiName | %WIKINAME% |
foswiki.loginName | %USERNAME% |
foswiki.wikiUserName | %WIKIUSERNAME% |
foswiki.serverTime | %SERVERTIME% |
foswiki.ImagePluginEnabled | %IF{"context ImagePluginEnabled" then="true" else="false"}% |
foswiki.MathModePluginEnabled | %IF{"context MathModePluginEnabled" then="true" else="false"}% |
var
before they are used.
var object = { foo: 'bar' }not
var object = { foo: 'bar', }
Use JQueryMetadata to integrate jQuery plugins into Foswiki.
Use JQueryLiveQuery to initialize your plugin for all html elements of a page. Otherwise content that is loaded asynchronously using ajax won't be initialized. LiveQuery will take care of that automatically.
Instead of
$(".jqPluginName").each(function() { // initializer });
use
$(".jqPluginName").livequery(function() { // initializer });
See JQueryMetadata for a more thorough example of useing metadata and livequery