Drupal – Add Javascript in Theme

As discussed before, we can add Javascript/JQuery in Drupal modules.
Drupal – Create Javascript for Module

So if the Javascript/JQuery is related to the layout and independent of the module, we should add it in the theme instead. For example, i want to have a alert box whenever a page is loaded, i should then add it to my current Drupal theme. The steps are listed below.

1. Open you current theme folder and add the following line in the theme.info.

  • scripts[] = js/theme.js


 

2. Make a new directory called js and create the theme.js in the theme folder.
theme.js

$(document).ready(function() {
  alert("hi");
});

 

3. Here is what you get when the Drupal page is shown in the browser.

 

Done =)

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.