1

What is the best way to load a js and css file on certain pages (not on every single page like happens when using the .info file)?

2 Answers 2

4

Drupal provides two functions: drupal_add_css() and drupal_add_js().

You can use these at any point in your code to add a CSS or JS file to the page load.

Sign up to request clarification or add additional context in comments.

2 Comments

For whatever reason this never works for me. I added it in the page--node-type.tpl.php file. Can it not go in there?
It won't work in a template file. Needs to be in a module file.
1

If the pages contain a form, you can use something similar to the following code:

$form['#attached']['css'] = array(
  drupal_get_path('module', 'ajax_example') . '/ajax_example.css',
);

$form['#attached']['js'] = array(
  drupal_get_path('module', 'ajax_example') . '/ajax_example.js',
);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.