Drupal JS Namespacing Blueprint with Search Text Replace


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



Copy this code and paste it in your HTML
  1. Drupal.behaviors.template = function(){
  2.  
  3. /**
  4. * Set namespace
  5. */
  6. var sitename = {
  7. /**
  8. * Initialise functions within the sitename namespace
  9. */
  10. init : function() {
  11. sitename.searchReplace();
  12. sitename.function2();
  13. sitename.function3.fInit();
  14. sitename.function4();
  15. },
  16.  
  17. /**
  18. * Puts title text in the field on the search box
  19. */
  20. searchReplace : function() {
  21.  
  22. var searchBox = $("#edit-search-theme-form-1");
  23. var searchBoxDefault = "Enter keywords";
  24. searchBox.attr("value", searchBoxDefault);
  25.  
  26. searchBox.focus(function(){
  27. if(jQuery.trim($(this).attr("value")) == searchBoxDefault) $(this).attr("value", "");
  28. });
  29.  
  30. searchBox.blur(function(){
  31. if(jQuery.trim($(this).attr("value")) == "") $(this).attr("value", searchBoxDefault);
  32. })
  33. },
  34.  
  35. /**
  36. * function 2
  37. */
  38. function2 : function() {
  39. //code here
  40. },
  41.  
  42. /**
  43. * function 3
  44. */
  45. function3 : {
  46. fInit : function() {
  47. //initialise function 3
  48. sitename.function3.helperfunction1();
  49. sitename.function3.helperfunction2();
  50. },
  51.  
  52. helperfunction1 : function() {
  53. //code here
  54. },
  55.  
  56. helperfunction2 : function() {
  57. //code here
  58. }
  59. },
  60.  
  61. /**
  62. * function 4
  63. */
  64. function4 : function() {
  65. //code here
  66. }
  67. } //end of sitename
  68.  
  69. /**
  70. * Initialise sitename
  71. */
  72. sitename.init();
  73.  
  74. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.