If I have this line in my view:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
Do I also put my actual script in my view?
<script type="text/javascript">
$(document).ready(function() {
alert("Hello world!");
}); </script>
Or do I put my actual javascript somewhere else and send it to the view somehow?
Thanks.
This is how i do it. For page specific javascript i include it in the page itself. For other javascript that would need to be run on most pages i put it in the global javascript, mine is called main.js.
So, as an example, say you would like to hide some_div on page load, and this div is on a specific page.
Then you would just do that in the page specific javascript.
An example of the main/global javscript file would be js that you want run on each page. So some examples are fadeIn animations for the content area, if all/most your pages have a content area and you would like to fade the content in on every page load, then the global java script would be a good place to put it so you don’t have repeated code. Another example is if you want to make all inputs with a
class="calendar"into a datepicker, then in your global js file you would just do.This way you don’t have to do that for each page and have repeated code. Long story short, if you are going to use the same js code in other pages too then might as well put it in a global js file.
In relation to codeigniter. What i did was create a “view partial” (really just a view) for all my js and css files. So in my view
js.phpi have:Then i just reference this view partial in the view page that i need to include the scripts i.e
This way i don’t need to manually include the javascript files i need for each view.