I have different css and js files related to different views. now how to load css and js conditionally or dynamically acording to views so that only the respective js or css loads to the browser.
so its simple just create condition on your fucntion than send your css or js file location to view.
so you can use this link.may be will help your answer
if something wrong i will give you the right answer
public function index() {
// View "css_js_view" Page.
//conditional
if(your condition){
$this->data = array( 'css' => site_url()."your css file", //this directory you css 'js' => site_url()."your js file" //this directory you js
);
}else{
$this->data = array( 'css' => site_url()."your css file", //this directory you css 'js' => site_url()."your js file" //this directory you js
);
}
$this->load->view('load_view',$this->data);
}
And the view is
<!--Load css and js-->
<link rel="stylesheet" type="text/css" href="<?php echo $css; ?>">
<script src="<?php echo $js; ?>"></script>
See more at: https://comshar.com/main/article/detail/22/104/0/how-to-load-cssjavascript-conditionally#sthash.cxop7DHB.dpuf
Add css and js files to the page from controller, and pass to view using data array. For loop the array and load css and java files from view. Example given below,
Controller section
$styles[1]['css'] = 'plugins/select2/select2.css';
$styles[2]['css'] = 'plugins/datatables/plugins/bootstrap/dataTables.bootstrap.css';
$data['styles'] = $styles;
$scripts[1]['js'] = 'plugins/select2/select2.min.js';
$scripts[2]['js'] = 'plugins/datatables/media/js/jquery.dataTables.min.js';
$data['scripts'] = $scripts;
View section
{if isset($styles)}
{foreach from=$styles item=v}
<link href="{base_url()}assets/{$v.css}" rel="stylesheet">
{/foreach}
{/if}
{if isset($scripts)}
{foreach from=$scripts item=v}
<script src="{base_url()}assets/{$v.js}"></script>
{/foreach}
{/if}
$add_js = array(
'filename' => array(BASEJS.'themejs/bootstrap-fileinput.js'),
'type' => 'import',
);
$footer = array(
'set_title' => MAINTITLE,
'javascript' => $add_js,
);
$get_footer = $this->settings->set_footer($footer);