summaryrefslogtreecommitdiff
path: root/ikiwiki/ikiwiki.js
blob: aebc5cf7ed66395ec8277a1e748d29184226f960 (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. var onload_done = 0;
  11. function run_hooks_onload() {
  12. // avoid firing twice
  13. if (onload_done)
  14. return;
  15. onload_done = true;
  16. run_hooks("onload");
  17. }
  18. function run_hooks(name) {
  19. if (typeof(hooks) != "undefined") {
  20. for (var i = 0; i < hooks.length; i++) {
  21. if (hooks[i].name == name) {
  22. hooks[i].call();
  23. }
  24. }
  25. }
  26. }
  27. function hook(name, call) {
  28. if (typeof(hooks) == "undefined")
  29. hooks = new Array;
  30. hooks.push({name: name, call: call});
  31. }
  32. function getElementsByClass(cls, node, tag) {
  33. if (document.getElementsByClass)
  34. return document.getElementsByClass(cls, node, tag);
  35. if (! node) node = document;
  36. if (! tag) tag = '*';
  37. var ret = new Array();
  38. var pattern = new RegExp("(^|\\s)"+cls+"(\\s|$)");
  39. var els = node.getElementsByTagName(tag);
  40. for (i = 0; i < els.length; i++) {
  41. if ( pattern.test(els[i].className) ) {
  42. ret.push(els[i]);
  43. }
  44. }
  45. return ret;
  46. }