<p>What is the best method for including a CSS or Javascript file for a specific node in Drupal 6.</p>

<p>I want to create a page on my site that has a little javascript application running, so the CSS and javascript is specific to that page and would not want to be included in other page loads at all.</p>

[{'answer_id': 61741, 'author': 'Evan', 'author_id': 6277, 'author_profile': 'https://Stackoverflow.com/users/6277', 'pm_score': 3, 'selected': True, 'text': '<p>The best solution I\'ve come up with so far is to enable the PHP input mode, and then call <a href="http://api.drupal.org/api/function/drupal_add_css" rel="nofollow noreferrer">drupal_add_css</a> and <a href="http://api.drupal.org/api/function/drupal_add_js" rel="nofollow noreferrer">drupal_add_js</a> as appropriate in a PHP block in the start of the body of your node.</p>\n'}, {'answer_id': 61744, 'author': 'MattBelanger', 'author_id': 655, 'author_profile': 'https://Stackoverflow.com/users/655', 'pm_score': 0, 'selected': False, 'text': "<p>You can have a custom template for that node (node-needsjs.tpl.php) which calls the javascript.  That's a little cleaner than using PHP right in the node body, and makes changes to content easier in the future.</p>\n\n<p><strong>EDIT:</strong> I don't think I was very clear above.  You want to name the template file node-(nodeid).tpl.php.  So if it was Node 1, call the file node-1.tpl.php</p>\n"}, {'answer_id': 61798, 'author': 'ceejayoz', 'author_id': 1902010, 'author_profile': 'https://Stackoverflow.com/users/1902010', 'pm_score': 2, 'selected': False, 'text': '<p>This should do the trick - a quickie module that uses the <a href="http://api.drupal.org/api/function/hook_nodeapi/6" rel="nofollow noreferrer">hook_nodeapi</a> to insert the JS/CSS when the node is viewed.</p>\n\n<pre><code>function mymodule_nodeapi(&amp;$node, $op, $a3 = NULL, $a4 = NULL) {\n  // the node ID of the node you want to modify\n  $node_to_modify = 6;\n\n  // do it!\n  if($op == \'view\' &amp;&amp; $node-&gt;nid == $node_to_modify) {\n    drupal_add_js(drupal_get_path(\'module\', \'mymodule\') . \'/mymodule.js\');\n    drupal_add_css(drupal_get_path(\'module\', \'mymodule\') . \'/mymodule.css\');\n  }\n}\n</code></pre>\n\n<p>This avoids security issues with enabling the PHP input filter, and doesn\'t require a separate node template file which could become outdated if you updated the main node template and forgot about your custom one.</p>\n'}, {'answer_id': 67901, 'author': 'Inferis', 'author_id': 6251, 'author_profile': 'https://Stackoverflow.com/users/6251', 'pm_score': 4, 'selected': False, 'text': '<p>I\'d advise against using <code>hook_nodeapi</code> for that. Adding CSS and Javascript is related to layout so <code>hook_nodeapi</code> is not the place for it: use themeing. This way, you <em>can</em> override those files when you\'re going to develop a new theme. Doing that with the nodeapi approach would be a bit harder (you\'d have to search the js/css list for the files, remove them and replace them with your own).</p>\n\n<p>Anyway: what you need to do is add a node preprocess function that adds those files for you. You can do this either in a module or in a custom theme. For a module this would be:</p>\n\n<pre><code>function mymodule_preprocess_node(&amp;$variables) {\n  $node = $variables[\'node\'];\n  if (!empty($node) &amp;&amp; $node-&gt;nid == $the_specific_node_id) {\n    drupal_add_js(drupal_get_path(\'module\', \'mymodule\') . "/file.js", "module");\n    drupal_add_css(drupal_get_path(\'module\', \'mymodule\') . "/file.css", "module");\n  }\n}\n</code></pre>\n\n<p>or for a theme:</p>\n\n<pre><code>function mytheme_preprocess_node(&amp;$variables) {\n  $node = $variables[\'node\'];\n  if (!empty($node) &amp;&amp; $node-&gt;nid == $the_specific_node_id) {\n    drupal_add_js(path_to_theme() . "/file.js", "theme");\n    drupal_add_css(path_to_theme(). "/file.css", "theme");\n  }\n}\n</code></pre>\n\n<p>Don\'t forget to clear the cache, first. </p>\n\n<p>These functions are called before the node is themed. Specifing the js/css there allows for a cascaded approach: you can have the generic/basic stuff in the module and provide enhanced or specific functionality in the theme.</p>\n'}, {'answer_id': 628706, 'author': 'canen', 'author_id': 43785, 'author_profile': 'https://Stackoverflow.com/users/43785', 'pm_score': 3, 'selected': False, 'text': '<p>I use the preprocess functions but this has some issues. <code>$variables[\'styles\']</code> is usually set <em>before</em> the node preprocess function is called. In other words <code>drupal_get_css</code> is already called which makes you calling <code>drupal_add_css</code> useless. The same goes for <code>drupal_add_js</code>. I work around this by resetting the <code>$variables[\'styles\']</code> value. </p>\n\n<pre><code>function mytheme_preprocess_node(&amp;$variables) {\n  $node = $variables[\'node\'];\n  if (!empty($node) &amp;&amp; $node-&gt;nid == $the_specific_node_id) {\n    drupal_add_js(path_to_theme() . "/file.js", "theme");\n    drupal_add_css(path_to_theme(). "/file.css", "theme");\n    $variables[\'styles\'] = drupal_get_css();\n    $variables[\'script\'] = drupal_get_js();\n  }\n}\n</code></pre>\n\n<p>This seems to work for most cases. </p>\n\n<p>P.S. There\'s hardly any ever need to create a module to solve a theming problem. </p>\n\n<p>Cheers.</p>\n'}, {'answer_id': 3013992, 'author': 'ninusik', 'author_id': 356681, 'author_profile': 'https://Stackoverflow.com/users/356681', 'pm_score': 2, 'selected': False, 'text': '<p>This seems like a good solution:</p>\n\n<p><a href="http://drupal.org/project/js_injector" rel="nofollow noreferrer">http://drupal.org/project/js_injector</a>\nand\n<a href="http://drupal.org/project/css_injector" rel="nofollow noreferrer">http://drupal.org/project/css_injector</a></p>\n\n<p>It works when you want to insert inline code into something other than technically a node so there\'s no node id and no PHP input option available. Like I used it to inject small jQuery tweaks into a couple of admin pages. It works by path rather than node id.</p>\n'}]
['https://Stackoverflow.com/questions/61735', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6277/']