Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By BWPanda on
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
...
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.
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
I've used drupal_add_js()
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?
here's the community documentation page link, a good resource
Adding JavaScript to your theme or module
http://drupal.org/node/304255
ownsourcing.com - Drupal training
post your drupal_add_js()
post your drupal_add_js() line (let's see the parameters) and your js file (let's see if your js is OK) added.
Function: <?php
Function:
<?php drupal_add_js('sites/all/themes/brat/scripts/actaccom-switcher.js', 'theme'); ?>
File:
You can use it in
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:
You'll have to clear your theme cache in order for it to take effect, but it's a lot cleaner coding.
-Corey
But if I use the
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...)
how were you choosing which
how were you choosing which page to load it on before?
-Corey
I wasn't. It was loading on
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.
Actually, it should probably
Actually, it should probably be this instead:
Use the if() code to decide when to load the js.
-Corey
Still not working... I used
Still not working... I used
drupal_get_title() == 'Create Activity Accommodation'
in the if() code.remove the parameter 'theme'
remove the parameter 'theme' in your drupal_add_js().
tell me if this can help.
No change... Still doesn't
No change... Still doesn't work.
do u have firefox firebug
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.
Yes, Firebug installed; no, I
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...
if u want to add your js
if u want to add your js inside page-node-add-content-type.tpl.php,
Yay! Adding $scripts =
Yay! Adding
$scripts = drupal_get_js();
seems to have done the trick.Thanks mm167!
Pls re-read coreyp's first
Pls re-read coreyp's first reply.
credit should goes to coreyp.
My solution is same as what coreyp said.
bye.
lol. gotta love it when that
lol. gotta love it when that happens. I'm just glad the problem got solved.
-Corey
...
thanks dude, you learned me something new!
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Ah yes. I must have seen his
Ah yes. I must have seen his 'best practices' comment and ignored the rest of what he said :)
Sorry coreyp_1!
I think it's a little
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
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...
I have the same problem.and
I have the same problem.and slove it by using it .Thank you.
I don't understand your
I don't understand your message. You say you have the same problem but got it solved "by using it" - what do you mean?
That actually got it working
That actually got it working for me under D7
Thank you. It worked
Thank you. It worked perfectly
--
Qasim Zeeshan
http://qasimzeeshan.com