Drupal 6: defining js and css for particular pages

2019-07-20 20:13发布

问题:

I'm a drupal newbie...

When I define css and js at filename.info file, it takes effects for all pages. I have some particular css-js files for some particular pages, and I don't wanna slow site down by importing all these files for the pages actually no need these files.

How can I define js and css for particular pages?

Appreciate helps!!! thanks a lot!!!

回答1:

The Drupal API offers two functions drupal_add_css and drupal_add_js that you can use.

There's a gotcha, if you use these in fx the preprocess_page function where the $script and $styles variable already has been created, you need to overwrite it with, drupal_get_css or drupal_get_js



回答2:

You can always add specific JavaScript for a particular content type using the preprocess_node function in template.php of your theme:-

function template_preprocess_node(&$vars) {
  if ($vars['type'] == 'content_type') { 
    drupal_add_js(path_to_theme() . '/script.js');
  }
}

Similarly using drupal_add_css for stylesheets.