Drupal: Run simple JS Scripts on a specific node
User login
Thursday, December 9, 2010 - 20:09
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.
- Initialize global variable $base_url to gain access to functions base_path() and path_to_theme(), so that you can generate the script's path dynamically. ie
<?php global $base_url; ?>
- Proceed to generate the right relative path to your script ie
<?php print base_path().path_to_theme().'/js/myjs.js'?>
- Override the appropriate template file ie node.tpl.php
- You can now use your script's functions in the particular node ie
<input type="submit" value="" name="op" OnClick="clrForm()">
<head>
...
<?php print $scripts; ?>
<?php global $base_url; ?>
<script type="text/javascript" src="<?php print base_path().path_to_theme().'/js/myjs.js'?>">
</head>
Tags:
Drupal 6, Drupal theming, Drupal Quick Tips