Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter: How to include javascript files

Hello I just started working with CodeIgniter framework. My current directory structure is

Demo(Project name)
 +System
 +Application
   -Controllers
      demo.php
   +Model
   -Views
      view_demo.php
 -Js
    ajax.js
    jquery.js  

Please tell me how to include .js files in view_demo.php.

Thanks Raj

like image 953
Raj Avatar asked Oct 01 '10 16:10

Raj


People also ask

Where do I put JavaScript in CodeIgniter?

Adding JavaScript and CSS (Cascading Style Sheet) file in CodeIgniter is very simple. You have to create JS and CSS folder in root directory and copy all the . js files in JS folder and . css files in CSS folder as shown in the figure.

Where do I put CSS and JS in CodeIgniter 4?

APPPATH is the path to your application folder. If you want to create an assets folder in your CodeIgniter root directory, create an assets folder in root, open your assets folder, create a CSS file named style. css, put it into the CSS folder, and create your js file called script. js and keep it into js folder.

HOW include external php file in CodeIgniter?

Then in the middle of the file edit the two variables $application_folder and $system_path and make sure you're using an absolute path instead of a relative one. Then in your external PHP script just include the external. php file and use the $CI global object to access the whole codeigniter: include '../../external.


2 Answers

You need to use the base_url() to include the javascript file in your VIEW.

So, in the view_demo.php file:

<script type="text/javascript" src="<?=base_url()?>js/jquery.js" ></script>
<script type="text/javascript" src="<?=base_url()?>js/ajax.js" ></script>

You will need the URL helper loaded. To load the helper you need to put on your demo.php controller:

$this->load->helper('url');

You can also autoload on \config\autoload.php on the helpers array.

More info about base_url(): http://www.codeigniter.com/user_guide/helpers/url_helper.html#base_url https://codeigniter.com/user_guide/general/styleguide.html#short-open-tags

like image 184
ipalaus Avatar answered Oct 18 '22 04:10

ipalaus


You wouldn't include JS files within the PHP, they would be output as script tags within the HTML you produce which you may be producing as output from the PHP script.

As far as I know, there is no built in CodeIginiter function to include this output like there is for CSS using the link_tag() function provided by CI. I've added a function called script_tag() to the system/helpers/html_helper.php file from CI. The function is:

if ( ! function_exists('script_tag')) {
    function script_tag($src = '', $language = 'javascript', $type = 'text/javascript', $index_page = FALSE)
    {
        $CI =& get_instance();
        $script = '<scr'.'ipt';
        if (is_array($src)) {
            foreach ($src as $k=>$v) {
                if ($k == 'src' AND strpos($v, '://') === FALSE) {
                    if ($index_page === TRUE) {
                        $script .= ' src="'.$CI->config->site_url($v).'"';
                    }
                    else {
                        $script .= ' src="'.$CI->config->slash_item('base_url').$v.'"';
                    }
                }
                else {
                    $script .= "$k=\"$v\"";
                }
            }

            $script .= "></scr"."ipt>\n";
        }
        else {
            if ( strpos($src, '://') !== FALSE) {
                $script .= ' src="'.$src.'" ';
            }
            elseif ($index_page === TRUE) {
                $script .= ' src="'.$CI->config->site_url($src).'" ';
            }
            else {
                $script .= ' src="'.$CI->config->slash_item('base_url').$src.'" ';
            }

            $script .= 'language="'.$language.'" type="'.$type.'"';
            $script .= ' /></scr'.'ipt>'."\n";
        }
        return $script;
    }
}

Then in your PHP code you can do:

echo script_tag('content/js/jquery-1.4.2.js');
like image 27
RC. Avatar answered Oct 18 '22 03:10

RC.