I have been beating my head against this all day. I have an external JavaScript file that I need to run a fairly complex calculator. I want the run the calculator in a page node. Whenever I add the external js by using the drupal_add_js line in my template.php file it conflicts (I think) with some of the other js files.

The conflict is only apparent and an issue in the admin section and when creating content.

All I want to do is to add the js to the one page and nowhere else.

I have no idea which of Drupals other js files this thing isn't playing nice with and am not good enough at js to trouble shoot it anyway, so I figure my best bet is isolation. Anyone have a good idea how to do this?

Comments

mfer’s picture

try something like this to get it to load on just one page...

if ($_SERVER['REQUEST_URI'] == '/path/to/node') {
  drupal_add_js('somefile.js');
}

This will only load for the one path.
--
Matt
http://www.mattfarina.com

erikhanson’s picture

Thank you, thank you, and thank you. It worked!

I really appreciate it!

gurukripa’s picture

pls teach me how to make javascript tabbed pages...collapsible READ MORE etc...on the page...to prevent more page refreshes....this way...we can overcome some speed issues ..pls help...

i know nothing of javascript etc..so pls help me with basic instructions..and some examples to figure out.

mfer’s picture

geerlingguy’s picture

Thanks so much for the code! I just wanted to add a tidbit for those who might need a little help. For the code in the post above, you need to add it to your template.php file, and you might need to remove the opening <?php part. Also, change /path/to/node to your node's path (obviously), and change 'somefile.js' to the location of your file, relative to the site root. In my case, I had the .js file in my theme folder, so I put in /sites/all/themes/theme-name/somefile.js

__________________
Personal site: www.jeffgeerling.com

patbranch’s picture

I'm having issues adding JS files to a node. It just isn't adding them...

I've tried this in the node:

//add JS files
function drupal_add_js_more($data = NULL, $type = 'module', $defer = TRUE, $cache = TRUE) {
  if (!is_null($data)) {
    _drupal_add_js('dw_scroll/js/dw_event.js', 'inline', FALSE, $cache);
    _drupal_add_js('dw_scroll/js/dw_scroll.js', 'inline', FALSE, $cache);
    _drupal_add_js('dw_scroll/js/scroll_controls.js', 'inline', FALSE, $cache);
  }
  return _drupal_add_js($data, $type, $scope, $defer, $cache);
}

and these in thetemplate.php


switch($_SERVER['REQUEST_URI']) {
case '/node/12':
  drupal_add_js('drupal-5.7/dw_scroll/js/dw_event.js');
  drupal_add_js('drupal-5.7/dw_scroll/js/dw_scroll.js');
  drupal_add_js('drupal-5.7/dw_scroll/js/scroll_controls.js');
break;
}


if ($_SERVER['REQUEST_URI'] == '/node/12') {
  drupal_add_js('somefile.js');
  drupal_add_js('dw_scroll/js/dw_event.js');
  drupal_add_js('dw_scroll/js/dw_scroll.js');
  drupal_add_js('dw_scroll/js/scroll_controls.js');
}
patbranch’s picture

Does anyone have any idea why it isn't working?

patbranch’s picture

bump

WorldFallz’s picture

Why the '_' before drupal_add_js? I just add js to a node by using the php input type and adding the files with a drupal_add_js for each file. Works fine.

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

patbranch’s picture

You mean... simply like this in the node?

  drupal_add_js('http://pbmedia.us/portfolio/biking/AC_RunActiveContent.js');

It's not working...

WorldFallz’s picture

I've not tried it with an absolute URL but something like this:

<?php
drupal_add_js(drupal_get_path('theme', 'mytheme').'/myscript.js', 'inline');
?>

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

patbranch’s picture

Okay. Maybe I should try putting the files in my theme and linking them that way...

tenek’s picture

Just in case anyone else has the same issue, for my XAMP install the path to the node starts at localhost while the path to the script starts from the drupal directory. Not sure why but had to fiddle with it for a while before it worked. This is what worked:

if ($_SERVER['REQUEST_URI'] == '/drupal/?q=node/13') {
drupal_add_js('sites/all/radio_checker.js');
}

Question: This requires running a query for every page loaded throughout the site even though the script is only used on one page. Does anyone know of a less resource intense solution? For example, is it possible to just have a script load when needed, such as on a button click?

Thanks!