Raj Raj - 5 months ago 44
Javascript Question

Codeigniter: How to include javascript files

Hello I just started working with CodeIgniter framework. My current directory structure is

Demo(Project name)

Please tell me how to include .js files in view_demo.php.



You need to use the base_url() to include the javascript file in your VIEW.

So, in the view_demo.php file:

<script type="text/javascript" src="<?php echo base_url();?>js/jquery.js" ></script>
<script type="text/javascript" src="<?php echo base_url();?>js/ajax.js" ></script>

You will need the URL helper loaded. To load the helper you need to put on your demo.php controller:


You can also autoload on \config\autoload.php on the helpers array.

More info about base_url(): http://www.codeigniter.com/user_guide/helpers/url_helper.html#base_url