[support] $scripts Vs drupal_get_js()

Jonathan Hedstrom jhedstrom at opensourcery.com
Wed Jan 23 01:37:59 UTC 2008


Ronald Ashri wrote:
> Hi,
>
> I am trying to figure out a strange situation in Drupal 5.6
>
> If I use the following:
>
> <?php
>   drupal_add_js(
>     '$(document).ready(function(){
>     $("p").fadeIn("slow");
>     });',
>     'inline');   
> ?>
>
>
> This does not get added to the header automatically.
>
> If I add:
>
> print drupal_get_js() it then inserts all the scripts at the point
> where it finds the code (just after body) in page.tpl.php
>
> The only solution is to substitute print $scripts with print
> $drupal_get_js() in the head section.
>
By the time page.tpl.php is called, the $scripts variable has already
been defined, so using drupal_add_js won't work at that level. You can
either fix it the way you've already done, or redefine the $scripts
variable in your template.php file. Something along the lines of:

in _phptemplate_variables() function:
drupal_add_js(...new js...);
$vars['scripts'] = drupal_get_js();

or top level, outside of any functions:
drupal_add_js(...new js...);

This second method works since the template.php script is included
before the $scripts variable is defined...

Cheers,
Jonathan



-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: OpenPGP digital signature
Url : http://lists.drupal.org/pipermail/support/attachments/20080122/a2a60b77/attachment.pgp 


More information about the support mailing list