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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

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

Requirements::javascript issue

Go to End

9 Posts   12212 Views


Community Member, 42 Posts

11 January 2009 at 12:34pm

The following is my Page_Controller method for a new page type...

class RangePage_Controller extends Page_Controller {
	function init()


This puts the javascript include into the resulting HTML fine...however instead of putting it in the <head> section it is putting it just after the opening <body> tag. Any idea why this is?


Forum Moderator, 4102 Posts

11 January 2009 at 5:05pm

Scripts don't belong in the head of your document. They belong at the bottom of the page, before the closing body tag. As of 2.3 that's where they should be going. If it's not doing that, there's certainly nothing invalid about including the scripts outside of the head. But as a general best practice, you really want them at the bottom of your document, below all the structural markup.


Community Member, 31 Posts

24 February 2009 at 3:07am

Even if it's best practice to optimize loading times by putting your js files at the bottom of the page (btw, you can specify "defer" if you put your scripts in the head, it will already help a lot), it may have some side effects. For example, if you use a library like swfobject, you may see the content that is about to be replaced. Or if you use libraries like jquery, all your behaviours on your page may not be working properly until the library is loaded.

I guess it would be nice if it was possible to choose to include the scripts in the head or just before the body closing tag.


Forum Moderator, 5523 Posts

24 February 2009 at 9:25am

You can choose. By default we do best practice which is bottom of the page, if you notice something odd with that you can disable this feature by going


in your _config file.


Community Member, 97 Posts

5 November 2009 at 3:53pm

It seems, from this Javascript newbie's perspective, that it would be useful to specific per Javascript file whether it'll go in the body or the head.

For now, though, Requirements::set_write_js_to_body(false) is my friend. I'm using a jQuery slider effect. Yuk yuk otherwise!


32 Posts

22 February 2010 at 5:40am

+1 for setting it for individual files.

Also, this method isn't documented here:

Any reason why?


Community Member, 98 Posts

10 November 2010 at 3:56am

+1 again for being able to choose <head> or <body> on a per-file basis. I'm big on best practices, but there's usually a script or two I need to put into the head to avoid the aforementioned content flash issue.

Good to know about this option though!


Community Member, 4 Posts

2 February 2012 at 2:43pm

+1 again for being able to choose <head> or <body> on a per-file basis. FOUC related JS like modernizr or prefixree js should be after the CSS in the head, where as jQuery and other plugins should be don't in the body.

Even better if I could put a variable into my template for exactly where I want it to go in case I want to place something after the JS/CSS in the head but before the closing </head> tag and same goes for the </body> tag. This would also save you from HTML parsing for the placement.

Go to Top