I am trying to add my JQuery code to my drupal 7 site.

In my drupal based site i have image in the featured block. My JQuery code slides up the image after 2 seconds.

When i added the JQuery code to my page it is showing the JQuery code on the page.

Here is my code:

<script src="/drupal/sites/default/files/js/jquery.min.js"></script><script>

when i save the configuration, below codes are displayed in web page.


How can i solve this?


If you don't know how to add your js code to drupal, there are two ways:

  1. Create js file in your theme_folder/js, e.g, scripts.js, put your code into that file, open .info file of theme and add scripts[]=js/scripts.js
  2. Create a js file as I wrote above and open template.php of your theme and put

    drupal_add_js(drupal_get_path('theme', 'YOUR_THEME_NAME') . '/js/scripts.js', array('type' => 'file', 'group' => JS_THEME));

    in YOUR_THEME_preprocess_page(). (This is not an ideal way, but it shows how you can use a drupal_add_js() function)

also read documentation for more info.

  • 1
    Don't use drupal_add_js, always use ['#attached']['js'] in a render array when that option is available – Clive Dec 13 '13 at 11:46
  • 1
    @Clive Thanks for the advice. ['#attached']['js'] is especially important with forms (I already faced such a problem), because of validation doesn't apply form's drupal_add_js (if it has been used in form generation function).. – xurshid29 Dec 13 '13 at 11:53
  • Interesting, I didn't know that thanks for the tip – Clive Dec 13 '13 at 11:58
  • 1
    Kirill Cebotari from ProPeople posted a great article a few days ago comparing 7 different ways you can add JS to Drupal. – kyletaylored Dec 13 '13 at 14:23

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