=== "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
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
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:
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
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.
=== "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
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.
-----------------------------------------------------------------
We build engaging websites and intuitive designs that will benefit your business. Duvien
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:
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.
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?
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
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/...
Comments
yes, you can use
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
What page?
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)
lol, I'm confused. Your post
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
it depends on what page you're trying to add the js to
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:
Hope that helps!
--A
This is all true-- however
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
Ok, I see what you're saying
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:
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?
yep ;-) === "Give a man a
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
That seems a great help,
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
Most likely in your case,
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:
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.
I have some JavaScript that I
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
And it is not working... can you see what I'm doing wrong?
Thanks
Hi aasarava,Please can you
Hi aasarava,
...your javascript goes here...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}Thanksremove the arg(2) portion from the if-statement
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.
Great, Thanks:)
Great,
Thanks:)
Thanks Everyone!
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:
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!
Excellent. Nice work!
Excellent. Nice work!
and the page you needed the js being '/events/' right ?
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 )
Slight variant
Adds JS to all nodes of type 'book'
I'm new to coding. Does this
I'm new to coding. Does this go into the template.php? Do I need to create a custom module for it?
Figured it out! Read the
Figured it out! Read the Drupal Handbook and created a custom module. Thank you for this great thread!
Is there a way to add
Is there a way to add multiple js files?
Just call drupal_add_js() for
Just call drupal_add_js() for each js file you want to add.
good solutions
This is an awesome thread, just what I needed. I'll do a documentation page for this when I get some time.
Nicolas
-------------------------
Add JS Solutions for Drupal 7 and Drupal 8
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...