Using external js with .php extension in Codeigniter [closed]

自闭症网瘾萝莉.ら 提交于 2019-12-03 09:02:57
Broncha

You need to make a controller to get the script. May be js. Inside your controller you set the content type headers and load the view which contains above javascript. Lets say you have a controller called js

class Js extends CI_Controller{
  public function js_functions(){
    $this->output->set_header('Content-type: text/javascript');
    $data = array( 'messages' => $this->session->flashdata('message'));
    $this->load->view('jsfunc',$data);
  }
}

And you can load the script in the main view like this

<script src="<?php echo base_url('js/js_functions'); ?>"></script>

If you want to generate dynamic js, css files. Use standart MVC method. Make a folder in view folder with name js or css. Create js or css files as php view file. For example : slider.js.php.

Make a controller themejs.php. Forward all js or css files to this controller which you need, by using routing. And inside this controller make dynamic action, pass varibles to js, css view files. And print them with header

base_url() is a function part of the CI url helper. If you want to make use of it in that file and you don't want to create an instance of the $CI object, you will have to implement your own version of that function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!