How to implement javascript

Permalink
I am trying to create a simple carousel without creating a block. But the problem is, I am not entirely sure where javascript should be hand coded. I already know jQuery is saved in the the core and that javascript files should be referenced in the header.php file. However, what I need to know is where would I save the javascript that you would normally type within the head region of a standard HTML document?

So far I have tried the following...

Inside the header.php I am calling a plugin for the javascript:

<script type="text/javascript" src="<?=$this->getThemePath()?>/js/jquery.carousel.min.js"></script>

and inside a HTML block (just testing to see if I can add javascript on the fly):

<script type="text/javascript">
$(function(){
$("div.foo").carousel();
});

<div class="foo">

<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>

....
</ul>
</div>

At the moment this does not work. I'm just wondering if the javascript that I am pasting into the HTML block needs to be saved into javascript file for it to be called from the header.php???

I have previously made a similar post, but felt it wasn't specific enough to what I was asking for, so apologies if you may have come across something similar before.

 
Pritam replied on at Permalink Best Answer Reply
Any jquery needs to be added after <?php Loader::element('header_required'); ?>
in the header.php file

Also do not include the jquery.min script as this is already included in the concrete5 core
Ibatterbee82 replied on at Permalink Reply
Thanks for the reply.

So you're saying that I need to add


<script type="text/javascript">
$(function(){
$("div.foo").carousel();
});
</script>


after <?php Loader::element('header_required'); ?>

??

Thanks
Pritam replied on at Permalink Reply
yes that as well besides calling the script file.
Ibatterbee82 replied on at Permalink Reply
Great, kinda makes sense now. Do I need to nest this in PHP tags?

Sorry, I'm more of a designer than a developer - still learning. But your help is much appreciated :D
Pritam replied on at Permalink Reply
This does not need to be nested within php tags
Ibatterbee82 replied on at Permalink Reply
Great, thanks so much.
Ibatterbee82 replied on at Permalink Reply
JavaScript now working in header.

Thanks!
mrfan replied on at Permalink Reply
If you wanna load JS on specific pages you can use Page Attributs and check this attribut on such Pages enable the JS file...so you don't load it all the time in your Theme...

check this Threat very good Trick:
http://www.concrete5.org/community/forums/customizing_c5/adding-jav...

regards martin
Ibatterbee82 replied on at Permalink Reply
Great, I'll have to try this at some point too. Could be handy if there is a lot of script
mrfan replied on at Permalink Reply
even you could setup a different PageType that you can easy choose while you ad a new page for eg PageType "Slider-Template" and then this PageType loads the needed JS files for your HTML....

but this is not so good choice because if you change the theme you have to alter every PageType, too..
boomgraphics replied on at Permalink Reply
boomgraphics
I've got a moderately complex example of a header.php here:

http://www.concrete5.org/community/forums/themes/header-php-example...

Should give you ideas on where everything might go. ;-)