Add JS file on a certain page on Drupal7.

function yourThemeName_preprocess_page(&$variables) {
  // Is this a node addition page for the specific content type?
  // TODO: Adjust the content type to your needs
  // NOTE: Will only work for the node add form – if you want your js to be
  // included for edit forms as well, you need to enhance the check accordingly
  if (‘node’ == arg(0) && ‘add’ == arg(1) && ‘yourContentType’ == arg(2)) {
      // Add your js file as usual
      drupal_add_js(‘path/to/your/file.js’, ‘theme’);
     // Ensure that the addition has any effect by repopulating the scripts variable
     $variables[‘scripts’] = drupal_get_js();
  }
}

 Use preprocess_page, not preprocess_node for this, as javascript inclusion should happen in the page template.

Tags