I have some jQuery that affects my 'node/add/content-type' page, so I only want the javascript file added to that page.
Using Devel's Theme Developer, I found I could create a copy of the page template file called 'page-node-add-content-type.tpl.php', however, adding the javascript file via drupal_add_js() in that file doesn't seem to do anything...

I can add normal HTML which will show up on that page, so the .tpl.php file is working, but I'm not seeing that particular javascript file in the header. Adding the javascript file via the theme's .info file works though...

Any ideas?

Comments

Jeff Burnz’s picture

I would think that drupal_add_js wont work in page.tpl.php, its too late by then, normally I would use it in template.php.

BWPanda’s picture

I've used drupal_add_js() before in 'webform-form.tpl.php'... Does that work differently to page-*.tpl.php?

If I used it in template.php, how do I specify what page to load it for?

mm167’s picture

post your drupal_add_js() line (let's see the parameters) and your js file (let's see if your js is OK) added.

BWPanda’s picture

Function:
<?php drupal_add_js('sites/all/themes/brat/scripts/actaccom-switcher.js', 'theme'); ?>

File:

/**
 * This function toggles the visibility of taxonomy fields depending on the
 * chosen Activity or Accommodation 'Type' option.
 */

$(document).ready(function() {
  // Set initial visibility of taxonomy fields
  toggleTaxonomy();

  // Toggle taxonomy fields when different option selected
  $('form#node-form .form-item .form-radios .form-item label.option input.form-radio').change(toggleTaxonomy);
});

function toggleTaxonomy() {
  // Setup variables
  var activity_field = $('form#node-form fieldset #better-select-edit-taxonomy-1');
  var accommodation_field = $('form#node-form fieldset #edit-taxonomy-5-wrapper');

  // Get selected option
  var type = $('form#node-form .form-item .form-radios .form-item label.option input.form-radio:checked').val();

  // Toggle taxonomy fields
  if (type == 'accommodation') {
    activity_field.slideUp();
    accommodation_field.slideDown();
  } else {
    accommodation_field.slideUp();
    activity_field.slideDown();
  }
}
coreyp_1’s picture

You can use it in page.tpl.php, but the problem is that the $scripts variable has already been set.

If you are going to use it, then instead of using <?php print $scripts; ?> in your <head> section, use <?php print drupal_get_js(); ?>.

[EDIT]

Oh... the drupal_add_js() must come *before* drupal_get_js().

[EDIT #2]

It's actually better practice to include this sort of thing in a preprocess function in your template.php file:

<?php

function yourthemename_preprocess_page(&$variables) {
  drupal_add_js('sites/all/themes/brat/scripts/actaccom-switcher.js', 'theme');
  $variables['scripts'] = drupal_get_js();
}

?>

You'll have to clear your theme cache in order for it to take effect, but it's a lot cleaner coding.

-Corey

BWPanda’s picture

But if I use the preprocess_page() function, how do I specify what page to load it on?
If I can't specify a certain page to load the javascript file on, I may as well go back to loading the javascript through the theme's .info file (which works fine, but defeats the purpose of only loading the file when needed...)

coreyp_1’s picture

how were you choosing which page to load it on before?

-Corey

BWPanda’s picture

I wasn't. It was loading on all pages, hence the need for a way to load it on only the one page it's used on.

coreyp_1’s picture

Actually, it should probably be this instead:

<?php

function yourthemename_preprocess_page(&$variables) {
  if (/* whatever condition */) {
    drupal_add_js(base_path() . path_to_theme() .'/scripts/actaccom-switcher.js', 'inline');
    $variables['scripts'] = drupal_get_js();
  }
}

?>

Use the if() code to decide when to load the js.

-Corey

BWPanda’s picture

Still not working... I used drupal_get_title() == 'Create Activity Accommodation' in the if() code.

mm167’s picture

remove the parameter 'theme' in your drupal_add_js().
tell me if this can help.

BWPanda’s picture

No change... Still doesn't work.

mm167’s picture

do u have firefox firebug installed?
if have, do u see any js error?

can u check the loading order of your script and jquery?
u may check this using firebug or reading the html source.

if the jquery is loaded after your js file, u should see a js error and that's the source of trouble.

BWPanda’s picture

Yes, Firebug installed; no, I don't see any JS errors.

My script isn't appearing in the header at all. I can see where jQuery loads, and other .js files, but not my script. Therein lies the problem...

mm167’s picture

if u want to add your js inside page-node-add-content-type.tpl.php,

1. u have to add before the line     <?php print $scripts ?>
 
2. beside of using drupal_add_js(), u need to update the $scripts.
drupal_add_js('your.js');
$scripts = drupal_get_js();


PS1 : use devel theme, check if your page-node-add-content.tpl.php file is used.

PS2 : check if your.js is added in the html source or firebug.


BWPanda’s picture

Yay! Adding $scripts = drupal_get_js(); seems to have done the trick.
Thanks mm167!

mm167’s picture

Pls re-read coreyp's first reply.

credit should goes to coreyp.

My solution is same as what coreyp said.

bye.

coreyp_1’s picture

lol. gotta love it when that happens. I'm just glad the problem got solved.

-Corey

Jeff Burnz’s picture

BWPanda’s picture

Ah yes. I must have seen his 'best practices' comment and ignored the rest of what he said :)
Sorry coreyp_1!

tinem’s picture

I think it's a little confusing to understand could you please show me the code YOU ended using for just ONE page, please?

I need to load both CSS and JS for ONLY one page to this site http://www.tinemuller.dk/drupal/node/1156. I have made an external CSS-file but not a js file yet but will do it if I can figure out how it's all done. In the end I think it would be better to write a module for this but I don't know enough to do this yet but hopefully soon.

In the page I have

<?php
drupal_add_css( drupal_get_path('theme', 'garland') .'/directions_renderer.css');
?>

When I have got all this to functioning I will figure out how to get from this site to maps.google.com showing the directions and have the possibilites to print out the map.

edited 16:39: If you are interested you can follow my discussion about the original file in this forum http://groups.google.com/group/google-maps-js-api-v3/browse_thread/threa...

1hello2’s picture

I have the same problem.and slove it by using it .Thank you.

tinem’s picture

I don't understand your message. You say you have the same problem but got it solved "by using it" - what do you mean?

plato1123’s picture

That actually got it working for me under D7

<?php print $styles; ?>

<?php drupal_add_js('sites/all/themes/dbr_curated/js/dbr_curated_author_page.js'); ?>

<?php print drupal_get_js(); ?>

qasimzee’s picture

Thank you. It worked perfectly

--
Qasim Zeeshan
http://qasimzeeshan.com