How to add external css and javascript file in codeigniter?

by cortez.connelly , in category: PHP Frameworks , 22 days ago

How to add external css and javascript file in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dedrick , 21 days ago

@cortez.connelly 

To add an external CSS or JavaScript file in CodeIgniter, you can follow these steps:

  1. Place your CSS or JavaScript file in the assets folder within your CodeIgniter project directory. You can create separate folders for CSS and JS files within the assets directory.
  2. In the header section of your CodeIgniter view file where you want to include the CSS file, use the base_url() function to link to the external CSS file. For example:
1
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/css/style.css');?>">


  1. In the footer section of your CodeIgniter view file where you want to include the JavaScript file, use the base_url() function to link to the external JavaScript file. For example:
1
<script type="text/javascript" src="<?php echo base_url('assets/js/script.js');?>"></script>


  1. Ensure that you have loaded the URL helper in your controller or autoloaded it in the config file by adding 'url' in the autoload.php file:
1
$autoload['helper'] = array('url');


  1. Finally, make sure that you have set the base_url in the config.php file:
1
$config['base_url'] = 'http://localhost/yourproject/';


By following these steps, you can easily add external CSS and JavaScript files to your CodeIgniter project.