Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load javascript css files in codeigniter ci

i wanna migrate my website into CI. i just simply modified from ci sample file welcome.php in index() function , i load the view to show. however , i put many javascripts and css files in the header file . and call it by $this->load->view('header'); but i can not load the javascript files correctly! Can anyone give me some tips ? it;s hard to configure how to set the correct path.

<script type="text/javascript" src="/assets/javascripts/order.js"></script> 
<script type="text/javascript" src="../assets/javascripts/order.js"></script>   
<script type="text/javascript" src="../../assets/javascripts/order.js"></script>    

my controller code as following

    class Welcome extends CI_Controller {

public function index()
{
    $this->load->helper('url');     
    $this->base = $this->config->item('base_url');

    $this->load->view('header');
    $this->load->view('welcome_message');


}

}

belows are my folder structure

enter image description here

like image 844
newBike Avatar asked Aug 25 '12 04:08

newBike


People also ask

How load JavaScript file in CodeIgniter?

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

How to add CSS and js file in CodeIgniter 4?

Now create a folder named CSS in the public directory, and create a style. css fine and keep this file in the same folder you just created. Now create a js folder in the public directory, create a custom. js file, and keep the file in the same directory you have just created.

How will you add or load a model in CodeIgniter?

Auto-loading Models If you find that you need a particular model globally throughout your application, you can tell CodeIgniter to auto-load it during system initialization. This is done by opening the application/config/autoload. php file and adding the model to the autoload array.

Where do I put assets in CodeIgniter?

If you want to create an assets folder in your CodeIgniter root directory, create an assets folder in root, open your assets folder, create a CSS file named style. css, put it into the CSS folder, and create your js file called script. js and keep it into js folder.


1 Answers

put your assets folder with applications, system, assets

not in application and simple load the url helper class in controller where you call the header view part something like

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

and simply use something like this in your header file..
because $this->base_url() return the / folder..

<script src="<?php echo $this->base_url();?>assets/javascript/jquery.js"></script>

Changing the folder structure because access within the application folder is just for the core part that i know..

here is the link if you want to know more about URL Helper

like image 74
jogesh_pi Avatar answered Sep 28 '22 11:09

jogesh_pi