I’m familiar with the process of attaching JavaScript and CSS to specific forms and form items in Drupal 7 using the #attached attribute.
I’m running into an issue which that solves, but unfortunately I’m on a D6 installation.
Basically, drupal_get_form() is being called from the theme layer (in a custom template file). The form it’s printing has form_alter hooks which execute drupal_add_js() (and css), but because the drupal_get_form is being called in the theme layer, those JS files are being added too late, and therefore not being included.
What’s the best Drupal 6 solution to this problem?
As said in the question,
drupal_get_form()is called too late in the page building process for thedrupal_add_js()anddrupal_add_js()calls to have any effect. Callingdrupal_get_form()in the theme layer is usually not an issue, but in this case I’m assuming that in this case it’s called in apage-<something>.tpl.php. Becasue of CSS and JS addition happen after the execution oftemplate_preprocess_page()which build the$script,cssand$stylesvariables.A simple solution is to call
drupal_get_form()from your theme’s page template preprocessor and then re-build the$script,cssand$stylesvariables.A better solution would be to call
drupal_get_form()earlier, from a lower level template (such asnode.tpl.phpouuser.tpl.php). But the best would be to retrieve the form outside of the theme layer in a page, block, etc. callback function.