Javascript question

Events happening in the community are now at Drupal community events on www.drupal.org.
OldAccount's picture

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?

mradcliffe's picture

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

OldAccount's picture

I have this in my webform-form.tpl.php (not sure where I got this from):

<?php
  drupal_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!

OldAccount's picture

I changed webform-form.tpl.php to webform-form-351.tpl.php and used:

<?php
  drupal_add_js
(drupal_get_path('theme', 'hcl') .'/scripts/quickEasyLabels.js', 'theme');
?>

instead of:

<?php
  drupal_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

mradcliffe's picture

That usually happens to me too.

Central Ohio

Group categories

Topics

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: