2

Is it possible when creating a view in Codeigniter to use a command to register an external Javascript file to be included in the <head> when the final output is written?

I have a main view which contains many smaller sub-views (sidebar, adverts, other controls, etc). inside one or more of these smaller views i want to incorporate some javascript functionality. To keep things modular i would like to 'include' that Javascript inside the smaller sub-views (without actually writing it to the html page at that sub-view's position (i want it in the head)). Is this possible?

4
  • Do you have a dedicated view just for your <head>? Commented Aug 5, 2011 at 11:08
  • The actual head tags are in the main view but there is a separate sub-view for some head content. Commented Aug 5, 2011 at 11:10
  • I'm not sure if this will help you, CodeIgniter's javascript class, codeigniter.local/user_guide/libraries/javascript.html. Commented Aug 5, 2011 at 11:13
  • In my case, I would put a condition in the main view or in a separate sub-view that would return true (and then register the external Javascript file) if one of my modules need it. Commented Aug 5, 2011 at 11:15

2 Answers 2

3

I ended up overiding and implementing my own Output class. (Placed in the 'application/core/' folder)

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Output extends CI_Output
{
    private $JavascriptFiles = array();
    private $JavascriptTags = "";

    private $CSSFiles = array();
    private $CSSTags = "";

    //Add a javascript file to the array.
    public function javascript($File = null)
    {
        if($File && !in_array($File ,$this->JavascriptFiles))
        {
            $this->JavascriptFiles[] = $File;
        }
    }

    //Add a css file to the array.
    public function css($File = null)
    {
        if($File && !in_array($File ,$this->CSSFiles))
        {
            $this->CSSFiles[] = $File;
        }
    }

    //Build the javascript tags
    private function buildtags()
    {
        if(!empty($this->JavascriptFiles))
        {
            foreach($this->JavascriptFiles as $File) {
                $this->JavascriptTags .= "<script type=\"text/javascript\" src=\"$File\"></script>\n";
            }
        }

        if(!empty($this->CSSFiles))
        {
            foreach($this->CSSFiles as $File) {
                $this->CSSTags .= "<link type=\"text/css\" href=\"$File\" rel=\"stylesheet\" media=\"all\" />\n";
            }
        }
    }


    //Override the main output function that sends content to the browser
    //and slap in the javascript and css where we need it.
    function _display($output = '')
    {

        $this->buildtags();
        $this->final_output = str_replace("{VIEW_SPECIFIC_JAVASCRIPT}", $this->JavascriptTags, $this->final_output);
        $this->final_output = str_replace("{VIEW_SPECIFIC_CSS}", $this->CSSTags, $this->final_output);

        // SNIP----
        // Rest of method code follows from here
}

I added the tags {VIEW_SPECIFIC_JAVASCRIPT} and {VIEW_SPECIFIC_CSS} into my <head> view at the appropriate places.

Then i can add javascript and css into the <head> from any view in my application by using the following methods inside the views:

$this->output->javascript("/js/mycoolscript.js");
$this->output->css("/js/mycoolcss.js");

Simple.

Sign up to request clarification or add additional context in comments.

Comments

1

Have you considered using the template library? http://williamsconcepts.com/ci/codeigniter/libraries/template/

It allows use to include JavaScript either in the head on a controller basis or inline.

$this->template->add_js('js/jquery.js');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.