Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
hass CreditAttribution: hass commentedComment #2
Damien Tournoud CreditAttribution: Damien Tournoud commentedThis is actually a documentation issue. This parameter is not (and has never been) linked with theme 'regions'.
The text should read:
Comment #3
hass CreditAttribution: hass commentedHow to make "page_top" a scope for adding JS code to this scope?
Comment #4
hass CreditAttribution: hass commentedhttp://drupal.org/node/171205 have nothing about scopes...
Comment #5
hass CreditAttribution: hass commentedAlso the same in here where scope is also named a region...
Comment #6
Damien Tournoud CreditAttribution: Damien Tournoud commentedA 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.
Comment #7
hass CreditAttribution: hass commentedIt 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.
Comment #8
Damien Tournoud CreditAttribution: Damien Tournoud commentedYou can still hook_page_alter() your way in, but I doubt it is really desirable for your module to add js code to regions.
Comment #9
hass CreditAttribution: hass commentedSeems also not working, see #812606: Cannot add inline JS code to regions via hook_page_alter()
Comment #10
marcingy CreditAttribution: marcingy commentedComment #11
SocialNicheGuru CreditAttribution: SocialNicheGuru commentedSorry 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
Comment #12
petergus CreditAttribution: petergus commentedIm also wondering this... For some reason
scrope => footer
works butscope => header
does not! strange..maybe because header means where it is defined in
print $scripts
?can we define our own scopes somewhere?
Comment #13
mikeytown2 CreditAttribution: mikeytown2 commentedFYI:
AdvAgg 6.x can do this and I believe AdvAgg 7.x can do this.