This is how jQuery is included in d7

(function ($) {
  // jquey codes here  

How i can include javascript code (not jquery) in d7?

4 Answers 4


If you want to write a plain JavaScript instead of JQuery, You can proceed with normal function declaration like

function sample() {

and go ahead. It is normal way of the doing things in javascript, More over jQuery is a framework of javascript and as said you can add js files in the traditional way

  • It's worth noting that Drupal uses behaviors for attaching (and removing) JS - see Chapabu's answer for an example and further reading.
    – Andy
    Commented Sep 11, 2012 at 11:34

jQuery is just a JavaScript library, so you can include plain JS inside the jQuery wrapper and it'll work fine. All the wrapper does is allow you to use the $ sign for jQuery instead of having to write jQuery('.xyz').

See the following example - standard JS inside a Drupal behaviour.

(function ($) {

  Drupal.behaviors.exampleModule = {
    attach: function (context, settings) {
      var ip = '<!--#echo var="REMOTE_ADDR"-->';
      alert("Your IP address is" + ip);


The Managing JavaScript in Drupal handbook page has LOADS of useful information about JavaScript in Drupal, it's definitely worth a read.


This is how jQuery is included so you can use the $ symbol. But you can write any piece of js code in that file without this part:

(function ($) {
  // jquey codes here  

But to include files, you can simply use drupal_add_js() in code, scripts[] = script.js in your_module.info or use $form['#attached']['js'][] = 'script.js' in a form array.


You can do that by implementing drupal_add_js() in your custom module.

  drupal_add_js(drupal_get_path('theme', 'mytheme') .'/mytheme.js');

Also you can add the files in your MODULE.info file by adding
scripts[] = youjsfile.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.