Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » how add javascript file

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 4826 Views
  • ojalà
    Avatar
    Community Member
    87 Posts

    how add javascript file Link to this post

    Hi!
    I try to modify menu of qa download tempate and to do this I need javascript file..so I have:

    chrome.css file that include css related to the menu
    chromejs directory that include chrome.js file whit javascript code

    The theme directory is caracterized by 3 folder:
    templates
    css
    images

    so where I put my file?
    I put chrome.css in css folder and chromejs in template (here there is page.ss where I call chrome.js in this way:
    <script type="text/javascript" src="chromejs/chrome.js">
    </script>
    )

    Then in pagse.ss, in the standar div I add

    <div class="chromestyle" id="chromemenu" id="Navcontainer">
    <% include Navigation %>
    </div>

    What is wrong?

  • Liam
    Avatar
    Community Member
    470 Posts

    Re: how add javascript file Link to this post

    Have you flushed the template cache afterward by visiting the page URL you're modifying and adding ?flush=1 to the end of it? i.e domain.com/page/?flush=1

    Another way is to add it in the controller of the page type - http://doc.silverstripe.com/doku.php?id=requirements

  • ojalà
    Avatar
    Community Member
    87 Posts

    Re: how add javascript file Link to this post

    Yes, I use correctly ?flush. I try also the second solution but something is wrong, why?

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: how add javascript file Link to this post

    The best way to add your CSS and Javascript is using Requirements::themedCSS() for CSS, and Requirements::javascript() for Javascript on the init() methods for your controllers.

    e.g. in Page.php

    class Page extends SiteTree {
       ...
    }

    class Page_Controller extends ContentController {

       function init() {
          parent::init();

          Requirements::themedCSS('myCustomCSSFile');
          Requirements::javascript('themes/' . SSViewer::current_theme() . '/javascript/jquery-1.2.6.pack.js');
       }

    }

    Make sure you have your theme directory structure like this:

    - themes
       - blackcandy
          - css
          - javascript
          - templates
       - mytheme
          - css
          - templates

    Make sure that in order to use "mytheme", for example, you change the SSViewer::set_theme() to SSViewer::set_theme('mytheme') inside mysite/_config.php

    Hope this helps!

    Sean

  • ojalà
    Avatar
    Community Member
    87 Posts

    Re: how add javascript file Link to this post

    Sorry but I have problem:
    my file are chrome.js and chromestyle.css

    I insert chrome.js in theme/mytheme/javascript and chromestyle.css in theme/css.

    then in mysite/code/Page.php and tutorial/code/Page.php I add

    Requirements::themedCSS('chromestyle.css');
    Requirements::javascript('themes/' . SSViewer::current_theme() . '/javascript/chrome.js');

    What is wrong?nothing seems go well.

    thanks..

    4826 Views
Page: 1
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.