Tiddlywiki Plugin Authoring: Detecting onload

Tiddlywiki plugins load on startup, early on in the whole boot sequence. I was wondering how to detect when elements are on the page and got some good advice in #tiddlywiki.

Turns out there’s no hooks, but a kind-of-hook is macros’ init() function. I didn’t want to use this, since I’m not creating a macro, though it would have been possible to create a “fake” macro – ie one that’s not intended to be called, but just does something in its init(). However, I went for the more general solution – hijacking (monkey-patching) restart():


  1. var origRestart = restart;
  2. window.restart = function() {
  3.   origRestart();
  4.   ... some code which can assume elements are on the page ...
  5. }

mmahemoff: hi, i’m writing a plugin that needs to load as soon as the page is ready…is there an event handler for that?
mmahemoff: specifically, my PageTemplate contains an element and I want my plugin to do document.getElementById(“element”)
Ace_NoOne: mmahemoff: well, there’s a ticket for those kinda hooks – but no implementation so far AFAIK
Ace_NoOne: a hacky way would be to write a macro that’s hidden in (executed by) some PageTemplate element
jayfresh: mmahemoff: there is this:
jayfresh: var startingUp = false;
jayfresh: that’s in the global space and is true during main()
jayfresh: and false the rest of the time
Ace_NoOne: FWIW, http://trac.tiddlywiki.org/ticket/484
Ace_NoOne: has some links
Ace_NoOne: specifically: http://www.tiddlywiki.org/wiki/Dev:Startup_Phases mmahemoff: great tips, thanks :)
Ace_NoOne: mmahemoff: macros’ init function run after refreshDisplay
mmahemoff: ok, that ought to do it in my case
mmahemoff: in the more general case, it sounds like hijacking restart() is the typical pattern?
Ace_NoOne: mmahemoff: saqimtiaz1 probably knows best
saqimtiaz1: mmahemoff: standard practice is to hijack restart. Not pretty but the best we have right now

(Snipped some discussion related to a separate thread.)

One thought on “Tiddlywiki Plugin Authoring: Detecting onload

  1. Pingback: Reflections from a TiddlyWiki Tiddler and Thoughts on a Guide for Web App Development with TiddlyWiki

Leave a Reply