0

I'm trying to find a good organization practice of my javascript (jquery) files when using CodeIgniter. I do a lot of ajax sites and many of my views are loaded in various controllers, I'd like to only load the necessary javascript for those views without duplicate code. How do y'all handle this issue?

2 Answers 2

2

Try using phil sturgeon's template library

http://github.com/philsturgeon/codeigniter-template/blob/master/libraries/Template.php

add a method called add_js and have it put itself as a variable just before build. Call this add_js method in each controller (in each method if needed) and define the js you want to be included.

then in your view do

<? foreach($js as $item): ?>
<script src="<?=$item?>"></script>
<? endif; ?>
Sign up to request clarification or add additional context in comments.

3 Comments

I haven't heard of this and will have to look into it. If it works out i'll check this as the answer, but right now I'd like to understand the concept behind it first.
basically it just abstracts all the things your likely to need into one nice library so you dont have to think of solutions manually for each page.
i've decided this is acceptable enough for me to consider as the answer :-) thanks for the help!
0

You could just create a js folder at the same level as the system folder. Then just include your JavaScript files from there, as you would on any web page.

1 Comment

i do that now, but I'm having trouble deciding what js files to create. Should I split them up by views or by pages? Or possibly create some script to only include the js needed per page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.