Welcome Guest, Not a member yet? Register   Sign In
Adding js, css files to header
#1

[eluser]BaRzO[/eluser]
Hi All,

I am trying to manage js, css file on the fly.
For example welcome/index needs 2 js file and 1 css file but welcome/contact needs extra 1 js file and 1 css file. This dependencies changes because of some pages can have different jquery plugins. When user change any plugin in any page the needed files must loaded.
How can I manage this. Pls help me.
Thnks.
#2

[eluser]harman[/eluser]
hi ,
you can achieve this by
Code:
extends Controller

Look@ Ci System\application\libraries
than create MY_Controller.php

Look Like this System\application\libraries\MY_Controller.php

-----MY_Controller.php----
Code:
class MY_Controller extends Controller {
    
    public function __construct() {
                parent::__construct();
                $this->data['js'] = '';
    }
    protected function loadJs($bolload=true,$filename) {
        if($bolload==true && $filename!='')
        {
            $this->data['js'] .= $this->load->view("your Location/js.tpl.php", array('filename' => $filename), true);
        }
    }
}

--------------------js.tpl.php
Code:
<sc type="text/javascript" language="javascript" src="&lt;?php echo base_url() . 'your_Path/' . $filename . '.js'; ?&gt;"></sc>

----------------in Controller
Code:
class Home extends MY_Controller {
    function __construct() {
        parent::__construct();
    }
    
    function index()
    {
              $this->loadJs("jquery.js");
              $this->loadJs("my.js");

              $this->load->view('welcome_message',$this->data);
        }
}


--------------in your welcome_message View
Code:
&lt;html&gt;
       &lt;head&gt;
                   &lt;?php echo $js; ?&gt;
       &lt;/head&gt;
       &lt;body&gt;
           ......
       &lt;/body&gt;
&lt;/html&gt;

//Same way You Also Do With CSS

Note: i Placed "Script" to "sc" Please Correct That CI Editor Remove Script Word( Security Reasons)
#3

[eluser]BaRzO[/eluser]
Thnks for you reply Wink
#4

[eluser]cahva[/eluser]
..or use something like Carabiner which lets you add css and js you need from controller easily. BTW, js should go just before end of body tag and css to head. Its no biggie but pages "seems" to load faster because html is rendered before loading js if you put them to the end of page.
#5

[eluser]BaRzO[/eluser]
Thank you cahva i did not know that. I will check it.




Theme © iAndrew 2016 - Forum software by © MyBB