Drupal: Run simple JS Scripts on a specific node

· konordo

Let's say that you have a simple input form, on a specific node in your drupal site and you just want to reset it, using a button. There are a few ways you could go about it, but this one is probably the simplest one.

<ol>
<li>Initialize global variable <strong>$base_url</strong> to gain access to functions <strong>base_path()</strong> and <strong>path_to_theme()</strong>, so that you can generate the script's path dynamically. ie <pre><code>&lt;?php global $base_url; ?&gt;</code></pre></li>
<li>Proceed to generate the right relative path to your script ie <pre><code>&lt;?php print base_path().path_to_theme().'/js/myjs.js'?&gt;</code></pre></li>
<li>Override the appropriate template file ie node.tpl.php</li>
<li>You can now use your script's functions in the particular node ie <pre><code>&lt;input type="submit" value="" name="op" OnClick="clrForm()"&gt;</code></pre></li>
</ol>

<pre><code>
&lt;head&gt;
...
&lt;?php print $scripts; ?>
&lt;?php global $base_url; ?>
&lt;script type="text/javascript" src="&lt;?php print base_path().path_to_theme().'/js/myjs.js'?&gt;"></script>
&lt;/head&gt;
</code></pre>