http://api.drupal.org/api/function/drupal_add_js/7 says

scope: The location in which you want to place the script. Possible values are 'header' or 'footer'. If your theme implements different regions, you can also use these. Defaults to 'header'.

I tried to assign Google Analytics tracking code to the page_top and page_bottom regions, but it's not added in source code. I'm adding the JS code in hook_init(), but plan to do this inside hook_page_alter().

Comments

hass’s picture

Title: Scope does not work in drupal_add_js » 'scope' parameter does not work in drupal_add_js
Damien Tournoud’s picture

Version: 7.0-alpha5 » 7.x-dev

This is actually a documentation issue. This parameter is not (and has never been) linked with theme 'regions'.

The text should read:

scope: The location in which you want to place the script. Possible values are 'header' or 'footer'. If your theme implements different scopes, you can also use these. Defaults to 'header'.

hass’s picture

How to make "page_top" a scope for adding JS code to this scope?

hass’s picture

http://drupal.org/node/171205 have nothing about scopes...

hass’s picture

Also the same in here where scope is also named a region...

 *     If you need to embed a CSS file before any other module's stylesheets,
 *     for example, you would use CSS_DEFAULT - 1. Note that inline CSS is
 *     simply appended to the end of the specified scope <strong>(region)</strong>, so they
 *     always come last.
Damien Tournoud’s picture

A theme that would want to define a new 'scope' (but I guess this is totally useless), simply has to call drupal_add_js($scope) at the correct place.

We should simply fix the documentation and only mention 'header' and 'footer', because those are the only two actually useful.

hass’s picture

It would be much more better if it would work as it's currently documented. I really need to be able to add js code to regions.

Damien Tournoud’s picture

You can still hook_page_alter() your way in, but I doubt it is really desirable for your module to add js code to regions.

hass’s picture

marcingy’s picture

Status: Active » Closed (works as designed)
SocialNicheGuru’s picture

Status: Closed (works as designed) » Active

Sorry to re-open but it is not clear if this has actually been resolved.

#2 states:
scope: The location in which you want to place the script. Possible values are 'header' or 'footer'. If your theme implements different scopes, you can also use these. Defaults to 'header'.

scope options like header and footer in hook_js_alter do not coincide with the theme.

good explanation is given here: http://drupal.org/node/330985#comment-1095483:
The scope "header" does not add to the "header" region, it actually adds to "html_head"; proper region name would probably be "head".
The scope "footer" does not add to the "footer" region, it actually adds to "closure"; proper region name would probably be "closure".

Closure has been removed in Drupal 7 in favor of hidden regions page_top, page_bottom. http://drupal.org/update/modules/6/7#hook_footer explains the use of hidden regions.

So js added to footer used to goto closure and somehow ends up at the bottom of the html page.

Does anyone know how and which variable exactly?

Scope options in hook_js_alter do not coincide with hidden regions. $page['page_top'] or $page['page_bottom'] as described in http://drupal.org/update/modules/6/7#hook_footer, are not accessible by hook_js_alter.

So how can I define a scope of page_bottom for js? $page is not a variable for hook_js_alter.

How does this relate to adding inline js using hook_page_alter?

I have a related issue:http://drupal.org/node/1945222

petergus’s picture

Im also wondering this... For some reason scrope => footer works but scope => header does not! strange..
maybe because header means where it is defined in print $scripts?
can we define our own scopes somewhere?

mikeytown2’s picture

FYI:
AdvAgg 6.x can do this and I believe AdvAgg 7.x can do this.