Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6229827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:38:26+00:00 2026-05-24T09:38:26+00:00

Is it possible when creating a view in Codeigniter to use a command to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:38:27+00:00Added an answer on May 24, 2026 at 9:38 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible or any library available for creating .csv file in ObjC ?
How can I (if possible) get to the ModelMetadata from a view without creating
Is it possible to do the following without a creating a view in between?
Possible Duplicate: iPhone - file properties Hi all. i m creating an application which
I am creating (possible a lot) of objects as a result from a WDS
When creating a setup/MSI with Visual Studio is it possible to make a setup
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a
When creating a Style, is it possible to set the ControlTemplate property to a
In VBP its possible to define user defined actions by creating a custom COM
I remember it is possible to stop a website in IIS simply creating a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.