Can JQuery be used in page.tpl.php? Both .js files are found and loaded by the browser. Shouldn't I get a Hello World dialog?

<script type="text/javascript" src="<?php print $base_path ?>misc/drupal.js"></script>
<script type="text/javascript" src="<?php print $base_path ?>misc/jquery.js"></script>
<script type="text/javascript"><!--
$(document).ready(function(){
	alert("Hello world");
});
//-->
</script>

This snippet, placed in page.tpl.php, does nothing.

THANK YOU! -thejerz

Comments

nevets’s picture

Does your browser show a javascript error? If you look at the generated source are the src paths correct? If you are using firebug (or other javascript debugger can you see that the files are correctly included?

I dumped your snippet in my page.tpl.php (inside the head section) and it worked fine.

kourge’s picture

You have to include jQuery first, then drupal.js.

thejerz’s picture

Nevets and Kourge,

Thank you for your help.
1) I was manually inserting those two scripts into my theme, but then I discovered:

<?php print $scripts ?>

This was working yesterday; this morning, it won't print ANYTHING. No clue what is wrong. Ideas? Have either of you experienced this simply not working?

2) Since print scripts isn't working, I thought I would go back to manually inserting these two JS files in page.tpl.php. Following the advice of kourge, I flopped the order. I put this in the the page template:

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

Firebug returns this:

$(document).ready is not a function
$(document).ready(function(){

and Safari says:

Value undefined (result of expression $(document).ready) is not object.

If you're wondering: well, did Firefox or Safari just not find the JS files? Did you misplace them? No: in Firebug on the scripts tab, I can see the jquery.js file. Safari also had no troulbe finding the file. The first line of jquery.js from Firebug is this:

// $Id: jquery.js,v 1.6 2006/12/23 21:46:35 dries Exp $

Looks up to date.

CONCLUSION: I don't know how Firefox or Safari could load these two JS files and report that the function does not exist. If nothign else: why is print $scripts not working?

Very frustrated and out of ideas.

I appreciate your help. Opening this site tomorrow at 3pm!!! Thanks - thejerz

thejerz’s picture

I suspected something funky and reinstalled Drupal on my local machine. Nothing changed; however, this problem could be in a module (?) or database (?), two areas I can't easily reinstall per se.

On a separate machine running Drupal, print scripts does work, I tried it, and this came out:

<script type="text/javascript" src="/misc/jquery.js"></script>
<script type="text/javascript" src="/misc/drupal.js"></script>
<script type="text/javascript" src="/modules/dblclick/dblclick.js"></script>
<script type="text/javascript">var dest = "&dbldestination=admin";</script>

Which is exactly what I expect, and want. The question now is: why will print scripts not work on my test site? Print styles does work. Is there any way a module could interfere with this output? I've disabed everything I think might be problematic / is unnecessary.

nevets’s picture

Drupal only includes jquery.js and drupal.js IF some module requests that some other javascript be loaded. If no such request is made, the $scripts will be empty and the print will appear not to work. To test this out edit some content that has collapsible fields sets, are the jquery.js and drupal.js files being loaded?

As for the error, I have noticed that if I get certain errors I need to refresh the page sometimes twice before jquery.js is loaded correctly (which should get rid of you error with the ready() function).

thejerz’s picture

Great nevets. No one ever told me it loaded conditionally! :)

I actually always want it to load. If I made a drupal_add_js() would it always only load it 1x?

THANK YOU!!!

nevets’s picture

Correct, you could make a simple module do to this to pull in what you need. If you place it in the non-cache case of a menu hook it will always load.