Jump to:

3431 Posts in 1025 Topics by 864 members

Template Questions

SilverStripe Forums » Template Questions » Requirements::javascript issue

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

Page: 1 2
Go to End
Author Topic: 7859 Views
  • fishe
    Avatar
    Community Member
    42 Posts

    Requirements::javascript issue Link to this post

    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?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Requirements::javascript issue Link to this post

    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.

  • lekoala
    Avatar
    Community Member
    30 Posts

    Re: Requirements::javascript issue Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: Requirements::javascript issue Link to this post

    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.

  • tobych
    Avatar
    Community Member
    97 Posts

    Re: Requirements::javascript issue Link to this post

    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!

  • streetdaddy
    Avatar
    32 Posts

    Re: Requirements::javascript issue Link to this post

    +1 for setting it for individual files.

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

    Any reason why?

  • timwjohn
    Avatar
    Community Member
    98 Posts

    Re: Requirements::javascript issue Link to this post

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

  • joelpittet
    Avatar
    Community Member
    4 Posts

    Re: Requirements::javascript issue Link to this post

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

    7859 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.