0

I'm recoding one site (D7) where I stumbled upon statically added jquery (using a script tag inside html.tpl.php that pulls jquery library from google CDN). I installed "jQuery CDN" module that does the same and expected that when I loose statically added script tag everything would stay the same, but the menu and slider don't work (menu is implemented using some internal jsddmenu.js file, also slider using slides.jquery.js). What's the difference between those two implementations, when both of them add:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>

to the html code?

1

Its better to add javascript using your: a) theme's .info file using scripts[] = "some/filename.js" b) in the theme dynamically ... such as mytheme_node_view() { drupal_add_js('file', 'some/filename.js'); } c) or in a custom module ... (similar to above)

depending entirely on your site's needs.

the reason its better to approach this this way is: 1) the theme's scripts[] array is cacheable and will attach the javascript to every page. 2) dynamically attaching it allows the script to be used only when needed. 3) you don't force the client to load anything they don't need. 4) if its attached in one of the above ways, it can be minified if needed --- allowing the client to download one big javascript file instead of a dozen separate ones.

| improve this answer | |
  • thanks for that clarification, but I was more interested in: why using Drupal's jquery library isn't working the same as adding the same script tag myself in html template file. – Dusan Markovic Jul 23 '12 at 9:01
  • You cannot add remote scripts in .info files. I'm not sure if you can do it in drupal_add_js either. But definitely using api.drupal.org/api/drupal/includes!common.inc/function/… Caching is the main reason, and theme updates is another reason. – AyeshK Jul 23 '12 at 9:49
  • If it's working when added in the html template and not when added correctly, I would double-check that the caches have been cleared after adding it. – beth Sep 27 '12 at 2:46
0

Why you should not add script in your tpl because Drupal has theme layer access and module layer access. It's for Drupal caching performance reason.

Have you read about how to work with Javascript & jQuery ?

http://drupal.org/node/171213

You can check this option to use Google CDN http://yourdrupal.site/admin/config/development/performance

If you need uptodate version you can install http://drupal.org/project/jquery_update

| improve this answer | |
0

Its may related to lead and Lag of files, when you add script in tpl it will load after all scripts specified in head. jQuery in dependency for slider ...

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.