By jerseycheese on
I have been trying various ways of implementing drupal_path_js in order to properly add a JS file to the of my page template.
I've searched the entire internet, and the closest I think I've come to finding a solution is:
<?php drupal_add_js(drupal_get_path('theme', 'nameoftheme') . "js/minmax.js") ?>
Theoretically, this should get the path to this theme folder and output the
right? It outputs nothing at all. The path to this file is going to change, so it needs to be dynamic, hence the drupal_get_path(). Trying to not hardcode things here. The actual server path to the file is: /path/to/site/themes/nameoftheme/js/minmax.js Any ideas? I'd really appreciate the help!
Comments
oops.
couple of brain farts there... should be "properly add a JS file to the head of my page template" and "this should get the path to this theme folder and output the script, right?". I guess variables and other code gets stripped from the body.
Can you give us a little
Can you give us a little more information ?
You mention multi sites, but i don't see anywhere on your post where you say it's a problem.
So, can you tell us :
Sorry...
I was a little too quick with the post trigger there. Hope this info can help. Thanks!
What I'm trying to do is output a javascript line in the head of my drupal/themes/nameoftheme/page.tpl.php file. I need to drop the hardcoded stuff I have now and include this JS file the proper way, through drupal_add_js().
At first I tried
drupal_add_js(drupal_get_path('js/minmax.js')But that did not output my JS file to the page.
Then, I realized maybe it was a path issue, so I looked into how to print the path to the file before the filename itself (I needed to do this anyway because the page has to include this javascript file regardless of the URL, ie... not an absolute link... must be generated dynamically since the site will move around in time directory-wise).
So yes, since this is a multi-site Drupal installation, I'm using a global themes directory. The themes have to be globally available.
I found the following comment which I thought would solve my problem:
http://drupal.org/newsletter/2005/winter#comment-90435
Instead, it did nothing. No code was outputted with what I tried next:
drupal_add_js(drupal_get_path('theme', 'nameoftheme') . "js/minmax.js")Here's my Drupal directory structure:
drupal/
---core (core drupal files)
---modules (global modules)
---sites (site folders)
------nameofmysite (my site)
---------modules
---------themes
------------nameoftheme (this folder is actually just a link to drupal/themes/nameoftheme)
------nameofmysite2
---------modules
---------themes
------------nameoftheme2 (this folder is actually just a link to drupal/themes/nameoftheme2)
---themes (global themes)
------nameoftheme (my theme)
---------flash
---------images
---------js
------------minmax.js <---- file I'm trying to include via drupal_add_js()
------nameoftheme2
---------flash
---------images
---------js
page.tpl.php is processed very late
page.tpl.php is processed at a very late stage where no more drupal_add_js() requests are processed. What you want is a way to call drupal_add_js() a bit earlier.
In your theme's template.php add this:
That adds the JS before phptemplate processes page.tpl.php, then gives back control to phptemplate to continue normally.
..
alienbrain - I edited that to remove the name of the theme per the request of the original thread author. hope you don't mind.
--
Knaddison Family | mmm Beta Burritos
--
Morris Animal Foundation