How to include jQuery file in CodeIgniter?

I would like to use jQuery inside my CodeIngiter project. I do not know how to include js file.

I would like to have something like below in my view

<script src='<?php echo base_url().APPPATH ?>js/jquery.js'></script> <script src='<?php echo base_url().APPPATH ?>js/my-js.js'></script> 
+4
source share
5 answers

First, create a folder to host it. Then you can use a template system like this one .

This allows you to add js files around the world in the main template or on a controller / function basis using the syntax $this->template->add_js('assets/js/jquery.js');

+4
source

Codeigniter has a javascript class: http://codeigniter.com/user_guide/libraries/javascript.html

You will download the jquery library via:

 $this->load->library('jquery'); 

You can then paste this into your header section of your view:

 <?php echo $library_src;?> <?php echo $script_head;?> 

For additional javascript files, I usually create a resource folder in my application folder and then use the base_url function to link to files such as:

 <script src="<?php echo base_url('resources/name-of-js-file.js');?>" type="text/javascript"></script> 
+4
source

For base_url() need to load an auxiliary 'url'.

To archive this, go to application/config/autoloader.php and add it to the $autoloader['helper'] variable as follows:

 $autoload['helper'] = array('url'); 
+3
source

add the new js folder to your application folder, and in your views add this line

 <script type="text/javascript" src="<?php echo base_url();?>js/name of your js file"></script> 
+2
source

You include the JS file just like any other HTML output.

See where your JS is (say, its / js as root), and then in the <head></head> section add the following:

 <script type="text/javascript" src="/js/jquery.min.js"></script> 
+1
source

Source: https://habr.com/ru/post/1390365/


All Articles