Posted By

elightbo on 06/12/09


Tagged

javascript DOM load


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

madr


on dom ready


 / Published in: JavaScript
 

URL: http://brothercake.com/site/resources/scripts/domready/

Call in js then use the object constructor to define individual functions. You can either call a named function:

var foobar = new domFunction(myFunction);

Or write an anonymous function directly inside the constructor:

var foobar = new domFunction(function() { //code ...

});

  1. // DF1.1 :: domFunction
  2. // *****************************************************
  3. // DOM scripting by brothercake -- http://www.brothercake.com/
  4. // GNU Lesser General Public License -- http://www.gnu.org/licenses/lgpl.html
  5. //******************************************************
  6.  
  7.  
  8.  
  9.  
  10. //DOM-ready watcher
  11. function domFunction(f, a)
  12. {
  13. //initialise the counter
  14. var n = 0;
  15.  
  16. //start the timer
  17. var t = setInterval(function()
  18. {
  19. //continue flag indicates whether to continue to the next iteration
  20. //assume that we are going unless specified otherwise
  21. var c = true;
  22.  
  23. //increase the counter
  24. n++;
  25.  
  26. //if DOM methods are supported, and the body element exists
  27. //(using a double-check including document.body, for the benefit of older moz builds [eg ns7.1]
  28. //in which getElementsByTagName('body')[0] is undefined, unless this script is in the body section)
  29. if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null))
  30. {
  31. //set the continue flag to false
  32. //because other things being equal, we're not going to continue
  33. c = false;
  34.  
  35. //but ... if the arguments object is there
  36. if(typeof a == 'object')
  37. {
  38. //iterate through the object
  39. for(var i in a)
  40. {
  41. //if its value is "id" and the element with the given ID doesn't exist
  42. //or its value is "tag" and the specified collection has no members
  43. if
  44. (
  45. (a[i] == 'id' && document.getElementById(i) == null)
  46. ||
  47. (a[i] == 'tag' && document.getElementsByTagName(i).length < 1)
  48. )
  49. {
  50. //set the continue flag back to true
  51. //because a specific element or collection doesn't exist
  52. c = true;
  53.  
  54. //no need to finish this loop
  55. break;
  56. }
  57. }
  58. }
  59.  
  60. //if we're not continuing
  61. //we can call the argument function and clear the timer
  62. if(!c) { f(); clearInterval(t); }
  63. }
  64.  
  65. //if the timer has reached 60 (so timeout after 15 seconds)
  66. //in practise, I've never seen this take longer than 7 iterations [in kde 3
  67. //in second place was IE6, which takes 2 or 3 iterations roughly 5% of the time]
  68. if(n >= 60)
  69. {
  70. //clear the timer
  71. clearInterval(t);
  72. }
  73.  
  74. }, 250);
  75. };

Report this snippet  

You need to login to post a comment.