Posted by OldAccount on December 2, 2010 at 3:59pm
I feel silly asking this, but I'm new to writing my own Javascript. I just finished some code to enhance a Webform, have the .js file in my theme folder and Javascript optimization is turned on.
The problem is that it's only needed on one page, and it's throwing a "document.getElementById... is null or not an object" error on the rest of the site. How do I get this script to only load on the specific Webform page instead of site-wide? I'm sure I'm making a newbie mistake here.
Comments
Where are you calling drupal_add_js?
Are you loading up the javascript file using the drupal_add_js() PHP function? It also depends on where you're calling your code to load up javascript. If it's in a node.tpl.php or page.tpl.php file directly, then that would get loaded a lot.
If you have a custom module, you could implement hook_form_alter(), and then only fire the JS based on a specific form id (or whatever you want).
Sorry to answer your Javascript question with PHP. ;-)
I have this in my
I have this in my webform-form.tpl.php (not sure where I got this from):
<?phpdrupal_add_js(path_to_theme() .'/scripts/quickEasyLabels.js');
?>
Nothing in the .info file or other tpl.php's so I can't figure out why it's still being called on the other pages.
Nevermind, fixed it!
I changed webform-form.tpl.php to webform-form-351.tpl.php and used:
<?phpdrupal_add_js(drupal_get_path('theme', 'hcl') .'/scripts/quickEasyLabels.js', 'theme');
?>
instead of:
<?phpdrupal_add_js(path_to_theme() .'/scripts/quickEasyLabels.js');
?>
Still not sure why it was being loaded on the entire site, but it's only loading on the single node now which is what I wanted.
Thanks for the help though Matthew! It never fails, soon as I post my question on here I end up figuring it out on my own... :)
That usually happens to me
That usually happens to me too.