Adding Javascript to a Drupal Page

ceo3141 - May 7, 2008 - 20:33

I am new to Drupal, and am converting an existing website that has some Javascript (including uses of Google Maps). I consulted the handbook, http://drupal.org/node/42544, and there is a fairly complicated procedure for adding Javascript. I feel I must be missing something -- there must be an easy way to add some simple Javascript and use Google Maps.

Any help is greatly appreciated! Thank you.

see this thread

WorldFallz - May 7, 2008 - 20:51

see this thread http://drupal.org/node/255418, if you have questions let me know...

===
"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

Thank you, I really

ceo3141 - May 14, 2008 - 15:23

Thank you, I really appreciate the responses on this forum. There are many very helpful experts like yourself.

But I am still confused -- that thread talks about putting it into a template file? Suppose I have a page, and I want some javascript to run when a visitor does something. Should I just put it on the page??? Why is this bad? If I do want to reference a file, say by some analogy to the html way:

How exactly do I do that in Drupal? Do I need to add the drupal_add_js function? Sorry, but it was really hard to follow the threads that we've both pointed out to do such a simple task and the Drupal page on "drupal_add_js".

Thanks.

you're welcome, but I'm

WorldFallz - May 15, 2008 - 01:57

you're welcome, but I'm hardly an expert, lol. I'm still learning myself.

If you truly only need it on the one page, you can always put it in the page itself with <script> tags-- just like any other html page. I've done that occasionally myself. you have to use the FULL HTML or PHP Code input format or add the script tag to the allowed tags for filtered html (which I don't recommend since anyone else using filtered html input format can then also insert script).

===
"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

How do you put a .js file

ceo3141 - May 15, 2008 - 19:45

How do you put a .js file into a page (because I want to use the Javascript file on several different pages)? Are there instructions 'for dummies' anywhere? I really did not follow the drupal_add_js thread. Thanks.

to put it in the body of the

WorldFallz - May 15, 2008 - 20:10

to put it in the body of the node you can do it as simply as:

<script type="text/javascript" src="/misc/jquery.js"></script>

If you're going to put it in more than a couple of nodes or need it in the head, you'll want to put in the page.tpl.php file for your theme after the

<?php
print $scripts
?>
line, for example:

<head>
  <title><?php print $head_title; ?></title>
  <?php print $head; ?>
  <?php print $styles; ?>
  <?php print $scripts; ?>
  <?php drupal_add_js(drupal_get_path('theme', 'mytheme') . '/myscript.js'); ?>
</head>

===
"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

How to add myscript.js to CCK-created node types --?

ceo3141 - May 16, 2008 - 14:55

I think I get it, except . . . suppose that I have created a new content type using CCK, and I would like to add myscript.js to all nodes of that type. How would I write the arguments for
drupal_get_path('theme', 'mytheme')?

I followed the Forum trail: http://drupal.org/node/255418 ---> http://api.drupal.org/api/function/drupal_get_path/5.

Specifically, the instructions for drupal_get_path( , ) say it take arguments that are themes or modules. If I had created a node type called "Chris_type_page" then I assume that would be the second argument, but what would be the first?
Thank you!

Actually, AFAIK, the

WorldFallz - May 16, 2008 - 18:39

Actually, AFAIK, the function statement itself doesn't need to change based on content type. If the js file is small, you may not care if it gets loaded regardless of content type. If you do, what changes is what file you put the statement in-- as long as you are using the same theme that statement should be the same.

Instead of putting it in page.tpl.php you could create a different page.tpl.php file for your content type and would put the drupal_add_js statement in your new page-[contenttype].tpl.php (which should be in the same theme directory and should be where your js file is also).

===
"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

Wow, that's great. It would

ceo3141 - May 19, 2008 - 14:02

Wow, that's great. It would have taken me a long time to arrive at that link for changing the template files! Thanks very much.

welcome ;-) === "Give a man

WorldFallz - May 19, 2008 - 15:26

welcome ;-)
===
"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

 
 

Drupal is a registered trademark of Dries Buytaert.