2

I'm in the process of working on my first code igniter project. I have some javascript that I need to write to initiate an instance of something on document ready. It's a one off, will never be called again. I need to include it in the head, but there may be a case where I need to do this at the end of the body. So, for example, lets say I have this:

<script>
alert('this is a one off alert');
</script>

What is the best practice in doing this stuff? Is it acceptable practice to put this in the controller? Does it need a model writing for it? Or do I need to create individual views for each script to be in MVC?

Thanks.

1
  • All your javaScript goes in view file. Commented Mar 18, 2013 at 16:43

2 Answers 2

1

JS is part of HTML so it should not be in controller or in model, so it should be in view, as you are using framework then you should keep it in a separate file, you can have separate file for each method in your controller or one single JS file each controller.

also you can make a base_controller in your core folder and extend all your controller with it, there you can set all the default JS and CSS. so if you call an index method for a controller it will load with default JS and CSS and if need to add any new just pass it from the controller as John B said

You can have this function in your helper file

/**
 * getting js files from public folder
 * @param array $js
 * @return string
 */
function js_tag($js) {
    $out = "";
    if (!empty($js)) {
        $js = array_reverse($js);
        foreach ($js as $j) {
            if (strstr($j, "http:") == "" && strstr($j, "https:") == "") {
                if ($j == 'tinymac') {
                    $out.='<script type="text/javascript" src="' . base_url() . 'jscripts/tiny_mce/tiny_mce.js"></script>' . "\n";
                } else {
                    $out.='<script type="text/javascript" src="' . base_url() . 'public/js/' . $j . '"></script>' . "\n";
                }
            } else {
                $out.='<script type="text/javascript" src="' . $j . '"></script>' . "\n";
            }
        }
    }
    $out .= '<script type="text/javascript"> var baseurl = "' . base_url() . '"</script>' . "\n";
    return $out;
}

/**
 * getting css files from public folder
 * @author Amir M
 * @param array $css
 * @return string
 */
function css_tag($css) {
    $out = "";
    if (!empty($css)) {
        $css = array_reverse($css);
        foreach ($css as $c) {
            if (strstr($c, "http:") == "" && strstr($c, "https:") == "") {
                $out.= link_tag(base_url() . "public/css/" . $c) . "\n";
            } else {
                $out.= link_tag($c) . "\n";
            }
        }
    }
    return $out;
}

and you can call it from controller

public function index(){
  $data['js'] = js_tag('one.js', 'two.js' , 'jquery.js');
  $data['css'] = css_tag('one.css', 'two.css' , 'jquery-ui.css');
$thuis->load->view('index' , $data);
}

Note: the above method will reverse the array so the last value in the array will come first on the page so keep the jQuery last in the array

in the header which is common on all website

<?php echo $js.$css?>
Sign up to request clarification or add additional context in comments.

Comments

1

generally the practice I use is this:

  1. All view files load a common header. And the view files are loaded at the end of the controller.

  2. You can either hardcode or pass the links for scripts you want to initiate from the controller. Hardcoding the script tags is self explanatory. If you want to do it a bit more dynamically, you could set up an array of script sources and just loop through them in the header.

so in the controller

$data['scripts'] = array();
$data['scripts'][] = 'http://yoursourcehere';

or if self hosting:

$data['scripts'][] = site_url('assets/js/yourscript.js');

then the view

if(isset($scripts))
{
   foreach($scripts as $script)
   {
      echo '<script type="text/javascript" src="'.$script.'"></script>';
   }    
}

so basically you can just put all your custom script in a separate file an load it this way. Its practical because it can load all your scripts at once in a common way.

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.