summaryrefslogtreecommitdiff
path: root/underlays/javascript/ikiwiki.js
blob: 1252f244f8429faab7d5e7dccdd175bdd37a33f7 (plain)
  1. // ikiwiki's javascript utility function library
  2. var hooks;
  3. // Run onload as soon as the DOM is ready, if possible.
  4. // gecko, opera 9
  5. if (document.addEventListener) {
  6. document.addEventListener("DOMContentLoaded", run_hooks_onload, false);
  7. }
  8. // other browsers
  9. window.onload = run_hooks_onload;
  10. function run_hooks_onload() {
  11. // avoid firing twice
  12. if (arguments.callee.done)
  13. return;
  14. arguments.callee.done = true;
  15. run_hooks("onload");
  16. }
  17. function run_hooks(name) {
  18. if (typeof(hooks) != "undefined") {
  19. for (var i = 0; i < hooks.length; i++) {
  20. if (hooks[i].name == name) {
  21. hooks[i].call();
  22. }
  23. }
  24. }
  25. }
  26. function hook(name, call) {
  27. if (typeof(hooks) == "undefined")
  28. hooks = new Array;
  29. hooks.push({name: name, call: call});
  30. }
  31. function getElementsByClass(cls, node, tag) {
  32. if (document.getElementsByClass)
  33. return document.getElementsByClass(cls, node, tag);
  34. if (! node) node = document;
  35. if (! tag) tag = '*';
  36. var ret = new Array();
  37. var pattern = new RegExp("(^|\\s)"+cls+"(\\s|$)");
  38. var els = node.getElementsByTagName(tag);
  39. for (i = 0; i < els.length; i++) {
  40. if ( pattern.test(els[i].className) ) {
  41. ret.push(els[i]);
  42. }
  43. }
  44. return ret;
  45. }