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.

Archive

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

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

how add javascript file


Reply

5 Posts   4863 Views

Avatar
ojalà

7 October 2008 at 11:26pm Community Member, 87 Posts

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?

Avatar
Liam

8 October 2008 at 5:48am Community Member, 470 Posts

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

Avatar
ojalà

8 October 2008 at 7:47pm Community Member, 87 Posts

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

Avatar
Sean

8 October 2008 at 8:12pm (Last edited: 8 October 2008 8:15pm), Forum Moderator, 921 Posts

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

Avatar
ojalà

10 October 2008 at 2:17am Community Member, 87 Posts

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