I want to use drupal_add_js and drupal_add_css on a per-page basis so I have added a 'scripts' text field to my basic content type and have set the Format Type to PHP.

It works beautifully until I turn js/css aggregation back on; then, none of the files I specified are loaded. Why?

  1. Because Drupal is still using the previously aggregated files (despite me flushing the cache many times).
  2. Because of some more sinister issue that will prevent me from adding my CSS/JavaScript in this manner.

I see other people doing the same thing here: https://drupal.org/node/290982.

  • 1
    can we see some code here? without it, it's hard to begin to help out. after that, then we can tear apart the security implications of your use of the php format type, too :)
    – Jimajamma
    Jun 6, 2013 at 22:25
  • Ignore my last comment I missed the bit about this being used in a custom field...the general rule is don't use drupal_add_js() in template files or in PHP filters in the UI, that will definitely stop it being aggregated as alexkb's answer notes
    – Clive
    Jun 7, 2013 at 15:00

2 Answers 2


Yes, drupal will definitely miss your custom JS scripts in it's js aggregation build phase, if you're loading JS on different pages. This is because by the time a user (or you) hits the other pages, the aggregated js file has already run previously, and created the cached JS file it thinks is all it needs. It uses this cache file until the cache is cleared or the crontab is run.

A couple of options are to either:

  1. try using the drupal_add_html_head() function to add the js when ever you want, or,
  2. write some js to detect what library is needed (perhaps based on a data attribute element in the markup), and then conditionally load the other js via the getScript method.

Good luck.

  • Ok, thats kind of what I suspected. My goal was to have an easy way to add js/css to a single page without editing templates. Sounds like I can either do it this way, or have drupal aggregate the files, but not both...
    – doub1ejack
    Jun 7, 2013 at 17:55
  • Well, what about this? Add a multi-select field to the content type of infrequently used css/js. Have the content-type template check that field and load associated files. That way I could have my cake & eat it too, no?
    – doub1ejack
    Jun 7, 2013 at 17:58
  • Those two options are suggestions for if you keep the JS aggregation on. If you turn it off, then just use the drupal-add_js() method, which will work fine. If you want to know how to call these functions for your content type, you probably want to open a new question for that.
    – alexkb
    Jun 8, 2013 at 1:14

Try AdvAgg. It has some useful things that help to understand what is going on. In this case I would add ?advagg-debug=1 to the end of the url and then look in watchdog for the corresponding advagg_debug entry.

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.