This is my problem.
I used Garland theme and I added a block like this $('.mymenu > li ').each(function(index) {alert(1)});
There was no problem when I stayed in admin/build/block.
However the problem happened when I entered the other pages. The firebug alert that it does not understand $('.mymenu > li ').
When I opened the source code and what I saw was


----- some lines for sheetstyle -----

I don't know where they are from because in garland theme there is only print $scripts; .

This is so weird.

After that, I did
In garland.info I added scripts[]=jquery.js

And I got this


----- some lines for sheetstyle -----

Anybody know how to fix this ?

Comments

nadongtae’s picture

This is my problem.
I used Garland theme and I added a block like this $('.mymenu > li ').each(function(index) {alert(1)});
There was no problem when I stayed in admin/build/block.
However the problem happened when I entered the other pages. The firebug alert that it does not understand $('.mymenu > li ').
When I opened the source code and what I saw was

<!--[if IE]>
  <script type="text/javascript" src="/nguonviet/misc/jquery.js"></script>
  <script type="text/javascript" src="/nguonviet/misc/drupal.js"></script>
  <script type="text/javascript" src="/nguonviet/sites/all/modules/nice_menus/nice_menus.js"></script>
  <![endif]-->

----- some lines for sheetstyle -----

I don't know where they are from because in garland theme there is only

print $scripts;

.

This is so weird.

After that, I did
In garland.info I added scripts[]=jquery.js

And I got this

<!--[if IE]>
  <script type="text/javascript" src="/nguonviet/misc/jquery.js"></script>
  <script type="text/javascript" src="/nguonviet/misc/drupal.js"></script>
  <script type="text/javascript" src="/nguonviet/sites/all/modules/nice_menus/nice_menus.js"></script>
  <![endif]-->

----- some lines for sheetstyle -----

<script type="text/javascript" src="/nguonviet/misc/jquery.js?S"></script>
<script type="text/javascript" src="/nguonviet/misc/drupal.js?S"></script>
<script type="text/javascript" src="/nguonviet/themes/garland/js/jquery.js?S"></script>
<script type="text/javascript">jQuery.extend(Drupal.settings, { "basePath": "/nguonviet/" });</script>

Anybody know how to fix this ?

dman’s picture

The recommended way to add js is drupal_add_js()
Interestingly, that does a little magic. If ANY scripts are loaded at all, jquery.js gets loaded first, as well.
If NO scripts are being loaded - jquery doesn't get sent.
This is what happened to you on 'normal' pages. Drupal didn't know that you wanted/expected jquery available.

And yea, adding it to the theme info would be expected to work. I'm not sure if it does however, or if caching may be an issue.
I sometimes set it with drupal_add_js in my template phptemplate_variables()
Or the quick way is to type it into your page.tpl.php HEAD yourself.

In the middle, a safe way will be to call

drupal_add_js('misc/jquery.js');

in your BLOCK code (PHP code) next to where you want to run your scripting.
This means
- it'll always have loaded when you want it
- It won't when you dont'
- it won't double up as drupal_add_js filters dupes.
- your block functionality is still totally independant from any theme hacks.

But about now I'd say make a copy of Garland and work from that before you make any more changes.

And of course TMTOWTDI

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

nadongtae’s picture

Thanks you a lot.

I have another idea that is I will create an empty javascript file such as scripts.js and add it into .info file like this scripts[]=scripts.js. Anh now every page I see jquery.js adding automatically. A cheat but it's ok. Don't worry about missing anymore.

beckyjohnson’s picture

I'm having a similar problem. I have some javascript that rotates linked images that I want to add to the front page. The code has internal javascript and and external file but I can't seem to get it to load or work. would I add the internal js into a block with the drupal_add_js code?