CodeIgniter - Adding JS & CSS

CodeIgniter - Adding JS & CSS

CodeIgniter - Adding JS & CSS

Adding a JavaScript and CSS (Cascading Style Sheet) file to CodeIgniter is very easy. You must create a JS and CSS folder in the root directory and copy all .js files to the JS folder and .css files to the CSS folder as shown in the picture.

For example, let's say you created one sample.js JavaScript file and one style.css CSS file . Now, to add these files to your views, load the helper url into your controller as shown below.

$this->load->helper('url');

Once the URL helper is loaded into the controller, simply add the following lines to the view file to load the sample.js and style.css files into the view as shown below.

   href = "css/style.css">

 

example

Create a controller named Test.php and save it to application/controller/Test.php

Create a view file named test.php and save it to application/views/test.php

 

  

      <metacharset="utf-8">

     

     

         href = "css/style.css">

     

  

        

  

      Click Here to execute the javascript function.

  

        

Create a CSS file called style.css and save it to css/style.css

body {

   background:#000;

   color:#FFF;

}

Create a JS file named sample.js and save it to js/sample.js

function test() {

   alert('test');

}

Edit the route.php file in application/config/rout.php to add the route for the above controller and add the following line at the end of the file.

$route['profiler'] = "Profiler_controller";

$route['profiler/disable'] = "Profiler_controller/disable"

Use the following URL in a browser to run the above example.

http://yoursite.com/index.php/test