Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[solved] Place Javascript after opening body tag while other JS stays at bottom?


Go to End


6 Posts   1505 Views

Avatar
derralf

Community Member, 28 Posts

15 September 2015 at 4:33am

Hi community,
usually JS is placed/loaded before the closing body tag. And some JS has to be in head (such as modernizr). So far so good.

But now i have a strange problem:
I have been told to place a code snippet (Google Tag Manager - JS and noscript-iframe) right after the opening body tag.
But when i do so all other JS-links that should stay at bottom suddenly move up so that they are right behind my new included snippet.

How can i prevent the "bottom-JS" to move up, when placing some other JS somewhere in the Template?

thanks in advance,
Ralf

Avatar
camfindlay

Forum Moderator, 267 Posts

15 September 2015 at 9:09am

Check out the javascript inclusion order documentation https://docs.silverstripe.org/en/developer_guides/templates/requirements/#inclusion-order

There are some things you can set to have more control over where the included files are placed.

Avatar
derralf

Community Member, 28 Posts

15 September 2015 at 8:24pm

Thanks, camfindlay!

Solution was:

Requirements::set_force_js_to_bottom(true);

...will force SilverStripe to write the Javascript to the bottom of the page body, even if there is an earlier script tag.

https://docs.silverstripe.org/en/3.1/developer_guides/templates/requirements/#javascript-placement

Avatar
brandhub

Community Member, 6 Posts

20 January 2016 at 11:05am

i am having the same problem. where should i add Requirements::set_force_js_to_bottom(true);? Which page?

Avatar
helenclarko

Community Member, 166 Posts

20 January 2016 at 12:25pm

Hey brandhub,

You should only need to throw that into your _config.php file.
This file is it "/mysite"

-helenclarko

Avatar
brandhub

Community Member, 6 Posts

20 January 2016 at 12:30pm

Thank you for your answer.
I also try to add in ../mysite/code/Pages/Page.php and seems working