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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Setting order of Javascript in templates


Go to End


2 Posts   1520 Views

Avatar
Liquid Edge Creative

Community Member, 56 Posts

11 October 2012 at 9:56am

Hey family,

I'm having trouble getting some javascript to run in one of my templates. If I add the following to the top of my HomePage.ss template, the associated Javascript is called in the head of the rendered page but the slideshow fails to work:

<% require javascript(mysite/javascript/fadeslideshow.js) %>

However if I add it to the head section of the Page.ss template, it works fine.

I also have the following in my Page.ss template:

<% require javascript(http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js) %>

On checking the source code of the rendered pages, I see that the slideshow works when fadeslideshow.js is positioned after the jquery.min.js, but fails when it is positioned before it.

So, how can I force Silverstripe to position the script in HomePage.ss before the script in Page.ss?

Avatar
gaethofs

Community Member, 9 Posts

4 November 2012 at 3:11am

Hi there!

Except when you're using the Layout folder, I don't think .ss-files are inheriting when they are on the same level.
Same for modules, the'll just be overwriting (I think).

I think it might also be a better approach to load your javascript at the bottom of your site instead of in the header.
Then I don't really get your question. So you want to put the script of HomePage.ss before the one of Page.ss,
although they don't inherit anything, at least not implicit? So unless you're using the Layout folder, I don't think this really matters.
You should just look at one .ss at a time and make sure the jquery is before the other scripts that are using it.

Did this help you in any possible way? Otherwise you should describe the problem with some more details cause I don't really get it :).

Greetings!