]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - underlays/javascript/ikiwiki/ikiwiki.js
Add license info to javascript underlay
[git.ikiwiki.info.git] / underlays / javascript / ikiwiki / ikiwiki.js
1 // © 2006-2010 Joey Hess
2 // Redistribution and use in source and compiled forms, with or without
3 // modification, are permitted under any circumstances. No warranty.
4 //
5 // ikiwiki's javascript utility function library
7 var hooks;
9 // Run onload as soon as the DOM is ready, if possible.
10 // gecko, opera 9
11 if (document.addEventListener) {
12         document.addEventListener("DOMContentLoaded", run_hooks_onload, false);
13 }
14 // other browsers
15 window.onload = run_hooks_onload;
17 var onload_done = 0;
19 function run_hooks_onload() {
20         // avoid firing twice
21         if (onload_done)
22                 return;
23         onload_done = true;
25         run_hooks("onload");
26 }
28 function run_hooks(name) {
29         if (typeof(hooks) != "undefined") {
30                 for (var i = 0; i < hooks.length; i++) {
31                         if (hooks[i].name == name) {
32                                 hooks[i].call();
33                         }
34                 }
35         }
36 }
38 function hook(name, call) {
39         if (typeof(hooks) == "undefined")
40                 hooks = new Array;
41         hooks.push({name: name, call: call});
42 }
44 function getElementsByClass(cls, node, tag) {
45         if (document.getElementsByClass)
46                 return document.getElementsByClass(cls, node, tag);
47         if (! node) node = document;
48         if (! tag) tag = '*';
49         var ret = new Array();
50         var pattern = new RegExp("(^|\\s)"+cls+"(\\s|$)");
51         var els = node.getElementsByTagName(tag);
52         for (i = 0; i < els.length; i++) {
53                 if ( pattern.test(els[i].className) ) {
54                         ret.push(els[i]);
55                 }
56         }
57         return ret;
58 }