Amazing Website Javascript Initialization Code Block v1


/ Published in: JavaScript
Save to your folder(s)

Link body class names to trigger javascript. It requires jQuery.


Copy this code and paste it in your HTML
  1. $(function(){
  2. //jquery references the body for grabbing class info and for general awesomness
  3. var body = $('body');
  4. jQuery.fx.interval = 5;
  5. //apply all methods as root level functions
  6. var root = this;
  7. //counter
  8. var i;
  9.  
  10. // the goods are in here.
  11. var pageScripts = {
  12. //init is called automatically
  13. //use to set up global stuff
  14. init:function(){
  15. }
  16.  
  17. };
  18. //determine if any of the pageScripts match the class of the body and call it/them
  19.  
  20. //keep track of how many properties have been executed
  21. var numRan = 0;
  22. //split up the classes used in the body
  23.  
  24.  
  25. //array of classes used in the body's class attribute
  26. var bodyClasses = body.attr("class").split(" ");
  27.  
  28. //call init
  29. pageScripts.init();
  30.  
  31. //loop through and execute all
  32. for(i in bodyClasses){
  33. if(pageScripts[ i ]){
  34. numRan++;
  35. pageScripts[ i ].apply(root, []);
  36. }
  37. }
  38.  
  39. log(numRan + " methods have been executed");
  40. });

URL: http://www.digitalsurgeons.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.