Javascript Initialization by page


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

This will initialize page depending on body tag id. Will load global init code for all pages and specific initialization per page.


Copy this code and paste it in your HTML
  1. var nameSpace = {
  2. onload : function() {
  3.  
  4. alert('global');
  5.  
  6. var page = document.body.id;
  7.  
  8. if(this[page] && typeof this[page].onload === 'function') {
  9. this[page].onload();
  10. }
  11.  
  12. },
  13. homepage : {
  14. onload : function(){
  15. alert('home');
  16. }
  17. },
  18. contact : {
  19. onload : function(){
  20. alert('contact');
  21. // onload stuff for contact us page
  22. }
  23. }
  24. };
  25.  
  26. //Execute global init and specific to page
  27. $(document).ready(
  28. function(){
  29. nameSpace.onload();
  30.  
  31. });
  32.  
  33.  
  34. The HTML
  35.  
  36. <html>
  37. <body id="homepage">
  38. This is home
  39. </body>
  40. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.