By thejerz on
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
Does your browser show a javascript error?
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.
jQuery first
You have to include jQuery first, then drupal.js.
Nevets and Kourge, Thank you
Nevets and Kourge,
Thank you for your help.
1) I was manually inserting those two scripts into my theme, but then I discovered:
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:
Firebug returns this:
and Safari says:
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:
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 $scriptsnot working?Very frustrated and out of ideas.
I appreciate your help. Opening this site tomorrow at 3pm!!! Thanks - thejerz
Update: print scripts works elsewhere
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:
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.
It may be working and you just don't know it :)
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).
RESOLVED
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!!!
Correct, you could make a simple module do to this
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.