By keff on
Hello, I am trying to add some jQuery powered goodness to my website, by adding jQuery code directly into header section of page.tpl in my template (in Drupal 6.1). Everything works fine, but only on pages where Drupal needs jQuery (like page with BUeditor, /node/??/edit with rollout menus etc.). On the other ones, jQuery isn't loaded (and i know it's smart default behaviour).
I tried putting
$js = drupal_add_js('misc/jquery.js', 'core', 'header');
print drupal_get_js('header', $js);
instead of
print $scripts;
into header, which worked, but it seems more of a hack to me and I would expect that this can have some nasty side effects (like loading jQuery twice maybe?).
Is there better way to tell Drupal I want to load jQuery on every page, regardless of whether drupal uses jQuery there?
Comments
Doesn't look too bad to me
Doesn't look too bad to me ... tho' I've not quite got my head round all the arguments to those functions. I'd have thought
would be sufficient? The 2nd line is how $scripts is defined anyway in http://api.drupal.org/api/function/template_preprocess_page/6.
I guess you've worked out the basic problem that you can't inject drupal.js into $scripts because $scripts is already defined ..! So your method sensibly just adds the jQuery (if not there already) to any other JS needed on the page and then grabs it all.
Unfortunately even implementing mytheme_preprocess_page(&variables) in template.php in your theme (as per http://drupal.org/node/223430) won't help since the template_preprocess_page() has already been run (and hence $scripts has been populated). Your best bet might be just to put the drupal_add_js() in a module hook that is run on every page view. From reading the Description it appears that hook_init() is the way to do this in D6 http://api.drupal.org/api/function/hook_init/6.
gpk
----
www.alexoria.co.uk
gpk
----
www.alexoria.co.uk
Thanks for the link to
Thanks for the link to hook_init()!
I will probably leave it as it is now, where it can be turned on and off in a template file, but the hook_init will surely come handy.
I had no scripts on the page
I had the problem of no javascript scripts being included on the page.
I added scripts[] = scripts.js to my_theme.info ( scripts.js is just a blank file) and it seemed to kick start the js process.
now it includes /misc/jquery.js, /misc/drupal.js and /themes/my_theme/scripts.js
as well as Query.extend(Drupal.settings, { "basePath": "/" });
all of which were missing until i add that blank script to my .info file
...
The documentation says you can put a 'script.js' file in your theme directory and it (and jQuery as well) automatically gets included.
Or, add a "scripts[] = my_script.js" to your theme's .info file.
It might be that you have to make drupal aware of these changes. Maybe visiting the theme administration page would do it. Or clearing Drupal's cache. You may wish to update the handbook pages with details if you gain some useful insights.
Worked for me
Copied misc/jquery.js to my theme directory and renamed to script.js
Worked immediately and perfectly on the next page load.
Yes jquery.js does get loaded twice on some pages as a result, but how can this cause a problem? It all seems to work fine as far as I can see.
I just ran into this today.
I just ran into this today. Instead of loading another jquery.js file you can just place an empty file named script.js into your theme folder. I found that if this file exists Drupal will load the jquery.js file as well...
=-=-=-=-=-=-=-
Miles
www.protitude.com
=-=-=-=-=-=-=-
Miles
This works for me..
I just updated our site and i encounter this problem, well thanks Prostitude...
your solution also works for me..thanks for this!
best,
arnel
Thanx for posting this I was
Thanx for posting this I was going mad trying to find out why it wasn't working!
________________
Live fast die young