By razzel on
My question is: how do I run a simple javscript?
I understand that hardcoding javascript into the page.tpl.php file is to be avoided. Instead javascript should be put into a script.js file in the chosen themes home directory. I created this script.js file and put the javascript code into it.
Then I did put the following line into the .info file:
scripts[] = script.js
But nothing happens. The page will not load the script. What am I doing wrong?
Comments
drupal_add_js should do the
drupal_add_js should do the trick.
And don't tell anyone, but occasionally i have resorted to <script> tags right in the node... but shhhh... lol.
===
"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
Should i put drupal_add_js
Should i put drupal_add_js into the .info file?
It would probably go into
It would probably go into the theme file where you need the script (page.tpl.php, you mentioned), and you would do something like:
drupal_add_js(drupal_get_path('theme', 'mytheme') . '/myscript.js');that's generally how i do it
that's generally how i do it too.
===
"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
I know that you can do that.
I know that you can do that. But we are not supposed to do that. Javascript sholuld go into the script.js file. But when I put them there, they dont work.
UPDATE
----------------------------------------------------------------------
Ok, I now know how to do it. Just put the script into the script.js file like this:
// JavaScript Document
window.open("http://www.drupal.org","","");
It works. :-)
I know that you can do
Huh? That's exactly what davedelong's code snippet above does.
It loads a separate javascript file named "myscript.js" (it could have just as easily been called "script.js") from your theme directory and adds it to the chain of javascript drupal is using. AFAIK, that's exactly the way you're supposed to do it.
===
"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
I am sure, but I did not
I am sure, but I did not know or understand that. I don't understand php. :-)
I'll break it up for you so
I'll break it up for you so you can understand what each part does:
drupal_get_path(): This takes two parameters: 1 - the kind of path you're looking for (in our case a path to a theme), and 2 - the name of the specific folder to look for ('mytheme'). It returns a path similar to: 'sites/all/themes/mytheme'
To that we concatenate '/myscript.js', now giving us: 'sites/all/themes/mytheme/myscript.js'
Then we use drupal_add_js() to tell drupal that when it displays the page, that we want to include the js file that we passing in as a parameter. One thing that still throws me for a loop every now and then is that drupal does a whole lot of stuff internally before it ever begins to output a single character to the screen. Therefore, we use functions and hooks to tell drupal to do extra stuff before it begins outputting, because by then it's too late to change anything.
HTH,
Dave
Thank you very much for your
Thank you very much for your explanation. Where does all this php code goes? Into page.tpl.php?
It goes into whichever
It goes into whichever template page you want to load the javascript. Page.tpl.php will load for any displayed page that's not covered by a different template page (like frontpage.tpl.php or page-user.tpl.php, for example).
Dave
I haven't tried it, but I
I haven't tried it, but I would think you could put it in template.php also (to cover all pages in cases where you have several page-xxx.tpl.php and don't want to edit each one).
===
"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
Location of script
Is the location of the script file arbitrary - is there any thing wrong or undrupal about saying:
You might be having trouble because you need to clear your cache
Hi razzel,
If you created a file called script.js in your theme directory (so it's somewhere like /sites/all/themes/yourtheme/script.js), and you also have the line
scripts[] = script.js
in your .info file, your script.js file should be showing up wherever
print $scriptsis called in your page.tpl.php file.If it's not showing up, try clearing your cache. If you have the devel module installed, the link should be something like /devel/cache/clear?destination=node%2F1. If clearing the cache from the devel module link doesn't work, try visiting your theme admin page (/admin/build/themes), which should clear the cache.
I've noticed the clear cache link created by the devel module doesn't always successfully clear the cache for me, but visiting the theme admin page always seems to.
in d6 there's also a clear
in d6 there's also a clear cache button at admin/settings/performance
===
"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