Is it possible to add javascript to a specific page, other than the home page?

Comments

WorldFallz’s picture

yes, you can use drupal_add_js in a php page.

===
"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." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

bkildow’s picture

Ok, that makes sense. What php page would I need to put this in to add to a specific page though? (or what conditional statement would I need to make)

WorldFallz’s picture

lol, I'm confused. Your post is to add javascript to a specific page-- so you would set the input format to "php code" and use the drupal_add_js function on the specific page where you want to add the js (and remember you can use jquery).

===
"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." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

aasarava’s picture

bkildow, the drupal_add_js() function is used in modules, where a module in question is building a form or other content to appear on a page and needs to insert some JavaScript into that page.

If the JavaScript code you want to insert into your specific page doesn't really have anything to do with a module, but is more of a theme customization, then you might be better off modifying a theme template file (page*.tpl.php file) and adding the JavaScript in the header.

For example, if the only page template file that you have is page.tpl.php, and the URL of the page you want to add JavaScript to is /my/page, then you could do something like this in page.tpl.php:

<head>
<?php if(arg(0) == 'my' && arg(1) == 'page' && arg(2) == null) {  //only display the script on /my/page ?>
  <script type="text/javascript">
    ...your javascript goes here...
  <script>
<?php } ?>
</head>

Hope that helps!
--A

WorldFallz’s picture

This is all true-- however you absolutely can use drupal_add_js in a php page. If you truly just need some javascript in the one page there's probably no reason to bother creating and modifying template files which, if you use multiple themes, will have to be done for each theme as well as maintained for theme upgrades across all themes.

===
"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." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

aasarava’s picture

Ok, I see what you're saying now. I think it might be easier to understand if we clarify some terminology:

- If you create a page of content by going to "/node/add" and using the "Page" content type or "Story" content type or some other similar type,
- and if that content type lets you change the filter to "PHP" so you can enter raw PHP code into the content body
- then you could indeed enter something like this in the body:

<?php
$myscript = 'my inline javascript, if inline javascript is what you want here...'; //could also refer to a file instead
drupal_add_js($myscript, 'inline'); //change the 2nd param to 'theme' if $myscript points to a theme .js file
?>

the rest of your page content...

Just be sure to change your filter on this content item to "PHP code" so that Drupal executes the lines of PHP rather than displaying the characters on the screen.

Sound about right, WorldFallz?

WorldFallz’s picture

yep ;-)

===
"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." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

onejam’s picture

That seems a great help, thanks. But what if you only have say /portfolio ( since the url is - http://domain-name.com/portfolio )

How would one change the code to accommodate this? I tried removing the arg(1) and arg(2) but nothing happens. I'm trying to use this in the template.php to load my js scripts.

Thanks,

NOTE: Sorry i was trying to reply to: http://drupal.org/node/304178#comment-996439

-----------------------------------------------------------------
We build engaging websites and intuitive designs that will benefit your business.
Duvien

aasarava’s picture

Most likely in your case, "portfolio" is an alias to some other URL, and arg(0) and arg(1) are returning the original URL components.

Here's a quick and dirty way to find out if the URL is getting translated to something else: You can stick the following code in your function (or at the top of page.tpl.php) temporarily and it will dump the values returned by arg(0) and arg(1) and arg(2) to the screen where you can see them:

<?php
$a0 = arg(0);
$a1 = arg(1);
$a2 = arg(2);
var_dump($a0, $a1, $a2);
?>

So add that code to your function in template.php or at the top of page.tpl.php (or whatever page template is used when the page at /portfolio is created). Then load the page at /portfolio and you should see the values of $a0, $a1, and $a2 on the page. If they look like, say, "node", "23", "null", then you know that /portfolio is actually an alias of /node/23.

ltrain’s picture

I have some JavaScript that I need to add to a specific page. When I enter your suggested quick and dirty method at the top of page.tpl.php (Drupal 6), I get:

string(4) "node" string(2) "14" NULL

I have tried

 if(arg(0) == 'node' && arg(1) == '14' && arg(3) == null) {

and

<?php if(arg(0) == 'string(4) "node"' && arg(1) == 'string(2) "14"' && arg(3) == null) {

Plus the JavaScript and <?php } 

And it is not working... can you see what I'm doing wrong?

Thanks

Aleksic’s picture

Hi aasarava,
Please can you show me... if I have 5 page e.g: my/page, my/page/2, my/page/3... and so on. How to use variable in your example.
Using this code:

if(arg(0) == 'my' && arg(1) == 'page' && arg(2) == null) { //only display the script on /my/page

...your javascript goes here... } Thanks
aasarava’s picture

Slobodan,

If you remove the arg(2) portion of the if-statement, everything within the if-statement will be executed for any pages that have my/page at the beginning of the URL path. So that includes my/page, my/page/2, my/page/3, etc.

if(arg(0) == 'my' && arg(1) == 'page') {  //display script on any pages in my/page/...
Aleksic’s picture

Great,
Thanks:)

bkildow’s picture

Thanks everyone, this has been a big help. I think I'm actually starting to get how Drupal works =).

After reading through the comments, and doing a little more research, here was the solution I came up with. In template.php:

function mythemename_preprocess_page(&$vars, $hook) {
  if('events' === arg(0)) {
    drupal_add_js(path_to_theme().'/js/events.js');
  }
  
  $vars['scripts'] = drupal_get_js();
}

The particular set of pages I'm using this for is actually a view, so this solution seems to work out nicely. Thanks for everyone's help!

aasarava’s picture

Excellent. Nice work!

dilari’s picture

If I understand correctly the page where you needed the script was something like:
http://www.yourdrupalinstal/events ?

right? ( I know it's 2 years after )

mikeytown2’s picture

Adds JS to all nodes of type 'book'

function mymodule_preprocess_page(&$variables) {
  if (isset($variables['node']) && $variables['node']->type == 'book') {
    // Add js
    drupal_add_js(drupal_get_path('module', 'mymodule') . '/mymodule.awesome.js');
    $variables['scripts'] = drupal_get_js();
  }
}
Gemini Lights’s picture

I'm new to coding. Does this go into the template.php? Do I need to create a custom module for it?

Gemini Lights’s picture

Figured it out! Read the Drupal Handbook and created a custom module. Thank you for this great thread!

Gemini Lights’s picture

Is there a way to add multiple js files?

bkildow’s picture

Just call drupal_add_js() for each js file you want to add.

ipwa’s picture

This is an awesome thread, just what I needed. I'll do a documentation page for this when I get some time.

Nicolas
-------------------------

hongpong’s picture

Since this is a prominent search result today, should add:

Javascript on specific pages in Drupal 7: http://drupal.stackexchange.com/questions/160008/how-do-i-add-custom-scr...

Javascript on specific pages in Drupal 8: https://www.drupal.org/docs/8/theming-drupal-8/adding-stylesheets-css-an...