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