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.

Template Questions

Requirements::javascript issue


Go to End
Reply

9 Posts   8066 Views

Avatar
fishe

11 January 2009 at 12:34pm Community Member, 42 Posts

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

class RangePage_Controller extends Page_Controller {
   
   function init()
   {   
      Requirements::javascript("mysite/javascript/jquery-1.2.6.pack.js");

      parent::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?

Avatar
UncleCheese

11 January 2009 at 5:05pm 4085 Posts

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.

Avatar
lekoala

24 February 2009 at 3:07am Community Member, 30 Posts

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.

Avatar
Willr

24 February 2009 at 9:25am Forum Moderator, 5511 Posts

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

Reaquirements::set_write_js_to_body(false);

in your _config file.

Avatar
tobych

5 November 2009 at 3:53pm Community Member, 97 Posts

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!

Avatar
streetdaddy

22 February 2010 at 5:40am 32 Posts

+1 for setting it for individual files.

Also, this method isn't documented here: http://api.silverstripe.org/default/Requirements.html

Any reason why?

Avatar
timwjohn

10 November 2010 at 3:56am Community Member, 98 Posts

+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!

Avatar
joelpittet

2 February 2012 at 2:43pm Community Member, 4 Posts

+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