I run into a strange thing. I followed the example in http://drupal.org/node/99973. I added two lines of javascript to the page.tpl.php in my theme


<head>
<script type="text/javascript" src="drupal/misc/drupal.js"></script>
<script type="text/javascript" src="drupal/misc/collapse.js"></script>
...

I can make the fieldsets colapsible when I edit a book page and use 'preview'. But when I view it in a normal way, the colapsible fieldsets are open and the open/close link for the fieldset are just plain text.

I noted that when editing the page, if I look at the source, their is links to

<script type="text/javascript" src="/drupal/misc/progress.js"></script>
<script type="text/javascript" src="/drupal/misc/upload.js"></script>

<script type="text/javascript" src="/drupal/modules/imce/imce_set_inline.js"></script>
<script type="text/javascript" src="/drupal/misc/autocomplete.js"></script>
<script type="text/javascript" src="/drupal/misc/collapse.js"></script>
<script type="text/javascript" src="/drupal/misc/textarea.js"></script>

But If i look at the source for a page in normal viewing mode, is it just the two links that I added in page.tpl.php

What am I doing wrong here?

Comments

dman’s picture

  1. Put on helmet
  2. Compare your
    <script type="text/javascript" src="drupal/misc/collapse.js"></script>
    with drupals
    <script type="text/javascript" src="/drupal/misc/collapse.js"></script>
  3. Repeat as needed
  4. Slap head

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

Boletus’s picture

Ahhh. Darn. :)

But still, I can only get it to work if I add the two lines

<script type="text/javascript" src="/drupal/misc/drupal.js"></script><br />
<script type="text/javascript" src="/drupal/misc/collapse.js"></script>

to the text body of the book page.

Is this really the way it should be done? I've read some post on drupal.org about the difficulty of adding javascript to pages. That things collide http://drupal.org/node/35678.

Any hints would be appreciated. I am spending lots and lots of hours trying to understand Drupal, but heavens know that it isn't that easy. :/

Boletus’s picture

Suddenly it worked with adding javascript to the page.tpl.php file. I looks alright, aside from that there is two arrows by the collapsible fieldsets clickable link. Something is clearly colliding.

dman’s picture

Remove the script includes from your node body.

Even better, use drupal_add_js() to add the scripts - this will avoid double-inclusions.

and clear your cache :). both database and browser. MYSQL:DELETE FROM cache

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

dman’s picture

The collisions are with any other javascript code that tries to set the body onload event in a non Drupal/Prototype.js way.
If you have no other Javascripts, that shouldn't be a problem.

Without knowing how global you want to make these includes, I can't advise the best place to put them. Drupal was naturally adding the scripts programatically only when it knew it was displaying a form that would use it. You are expected to do the same with your own module code.

Adding the scripts to the template directly should work, but it a bit of a shortcut. Not sure why it's not quite right for you yet. Validate your page?

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

Boletus’s picture

The two javascripts that I add to the page.tpl.php is drupals own, standard files from the misc folder.

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

I don't understand how I can get Drupal to load these two javascript files as default on a page?

dman’s picture

Did you not learn the lesson from your typo before?
You appear to have drupal in a subfolder... so src="/drupal/misc/drupal.js"

(!)

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

Boletus’s picture

No, no. I am not that stupid. How can manage which javascripts get loaded in a book page, or any page?

In the page.tpl.php file, there is
print $head
print $styles

How can I add the javascipts I want to the $head or $styles. I guess that is where they are supposed to be? There is some trouble with loading javascript explicably in a template file.

Perhaps I am bad at explaining what the problem is.

paris’s picture

Not working for me here.