By kinkajou


2011-10-09 08:23:02 8 Comments

How can I add JavaScript and CSS to my site?

2 comments

@Pierre Buyle 2011-10-09 09:44:37

The Managing JavaScript in Drupal 7 guide provides the information to add JavaScript to Drupal. The techniques presented there apply to both themes and (custom) modules:

Note that Drupal already include jQuery, jQuery UI and a bunch of other libraries so you don't, and shouldn't, add them yourself. Instead you should use drupal_add_library() or the #attached['library'] property.

The Working with CSS guide provides the information to add CSS to Drupal. Again, the techniques presented there apply to both themes and (custom) modules. With an exception: addition of style sheets from an .info files only works for a theme and not a module.

  • Add stylesheets[all][] = somestylesheet.css to a theme's .info file.
  • Use drupal_add_css() from module or theme's code.
  • Use #attached[css] property in a From API or renderable array.

@kinkajou 2011-10-09 13:14:26

do I need to enable php filter? And where do I add the js. Is it the body section with script tag? like <script></script> I need a simple example to get started. Very basic. Pls

@Pierre Buyle 2011-10-10 08:53:53

You don't add CSS or JavaScript as content of your site, so you don't do it when editing your content. If you need page specific JavaScript or CSS, you can do it from your theme's page preprocess hook. Adding styles (CSS) or behaviors (JavaScript) to your site should be done from code, not from the Web UI. This is way more maintenable and scalable and avoid you, or others, many headaches when updating the site later.

@drupal 2011-10-09 20:02:22

You can place your js file in your theme folder and add this js file to drupal by using drupal_add_js() in template.php preprocess functions.

Related Questions

Sponsored Content

5 Answered Questions

[SOLVED] How do I add JavaScript specific for a view?

1 Answered Questions

[SOLVED] Add JavaScript inside controller

0 Answered Questions

Javascript not loading in all links of the site

  • 2017-01-23 13:25:58
  • John Rey Tanquinco
  • 77 View
  • 0 Score
  • 0 Answer
  • Tags:   javascript

2 Answered Questions

[SOLVED] How can I use some Javascript to force a fieldset to stay open

5 Answered Questions

[SOLVED] Add JavaScript files to the administrative pages

1 Answered Questions

[SOLVED] Custom CSS and JavaScript

  • 2015-11-25 13:20:29
  • Tanmay Kadam
  • 85 View
  • 0 Score
  • 1 Answer
  • Tags:   javascript theming

5 Answered Questions

[SOLVED] how do I add javascript to a specific panel-page

0 Answered Questions

no css/javascript when anonymous

  • 2015-02-23 17:59:05
  • Guillaume Bois
  • 78 View
  • 0 Score
  • 0 Answer
  • Tags:   7 javascript theming

1 Answered Questions

[SOLVED] Drupal Add Javascript type=text/javascript

  • 2014-04-29 07:03:13
  • John
  • 647 View
  • 0 Score
  • 1 Answer
  • Tags:   javascript

2 Answered Questions

[SOLVED] How to add javascript to drupal 7?

  • 2013-06-29 10:55:38
  • Yinon Bitton
  • 791 View
  • 0 Score
  • 2 Answer
  • Tags:   7 routes javascript

Sponsored Content