> I tried my wiki with Google Chrome yesterday and found that it would
> not load correctly. I traced the problem back to a function I have
> running at the startup of the wiki.
> //{{{
> for (var i=0;i<2;i++) {
> }
> //}}}
> Is there a reason why a for loop is preventing the wiki from
> loading in its entirety?
During startup plugins are invoked by the core from a function called
loadPlugins(). This function performs several loops through an alpha-
sorted list of 'systemConfig' tiddlers (i.e., plugins) in order to
invoke each one in turn. The index variable used to control these
loops is, notably, named "i"...
While the first loop in this function is written using a *locally-
scoped* 'var i...' declaration,
for(var i=0; i<nPlugins; i++)
the index variable for two other loops in the loadPlugins() code seem
to have been inadvertently declared with an implicit *global-scope* by
omitting the 'var' from the syntax, like this:
for(i=0; i<nPlugins; i++)
for(i=0; i<toLoad.length; i++)
Unfortunately, in your errant plugin code, you also perform a loop
using the index variable, "i"... for which you *do* use the locally-
scoped 'var' declaration.
However, there are some subtle browser-specific differences in the
javascript variable scoping rules. In IE, it seems that, despite the
use of 'var i' in your code, the loop index is, in fact, globally-
scoped, while in FireFox and other browsers, the variable is correctly
handled with local scoping.
As a result, when your plugin code is loaded, it immediately alters
the index of the outer loop that controls the loading of plugins,
causing that loop to, in effect, jump back to almost the beginning of
the list of plugins and repeat the loading process again. Of course,
as soon as it reloads your plugin, the loop index is 'stepped on'
again, and the cycle repeats, infinitely.
The proper way to fix this is to correct the core code so that all the
loop indices are declared with local scoping, so that plugins can't
step on those values during load-time processing.
In the mean time, one workaround is to wrap the plugin's
initialization code within a function declaration and then immediately
invoke that function. This ensures that the scope of any variables
declared in that code is isolated within a locally-scoped function.
Something like this:
function myPluginInit() {
for (var i=0; ..... ) {
...
}
}
myPluginInit();
I've opened a ticket for this problem (see
http://trac.tiddlywiki.org/ticket/754),
so it should hopefully go away in the next release of the TW core...
-e
Eric Shulman
TiddlyTools / ELS Design Studios