Strategy to move javascript to the bottom in CodeIgniter?

On my site, I have one global Javascript file that includes jQuery and code for a dropdown menu, among other things. Many pages also have custom Javascript for minor interactions, tables, etc.

My current setup for each view is a file header.phpthat basically covers everything from doctype to the start of the content, the view file for a specific page and footer.phpclosing the page.

Currently global.jsassociated with <head>. For performance, we have to put JS at the very bottom of the page, but I cannot figure out how to do this. I could add a full script line for global.jsusing a custom script block, but that means I have to add it to every page, even if there is no other Javascript. Any better way to move JS to the bottom?

+5
source share
7 answers

JS nowdoc (php 5.3.0+), script , . Nowdoc , , JS PHP / .

someview.php:

<?php 
$custom_js = "
alert('custom js ran');
";
?>

<?php 
$custom_js = <<<'CUSTOM_JS'
alert("custom js ran (i'm in a nowdoc!)");
CUSTOM_JS;
?>

footer.php:

<?php if(isset($custom_js)) { ?>
    <script><?php echo $custom_js; ?></script>
<?php } ?>

2: , javascript , javascript , PHP file_get_contents() $custom_js .

Edit: , Carabiner JS CSS. . , , global.js , Carabiner / . , JS CSS CI.

+2

, - - , js?

:

$this->load->view("header.php");
$this->load->view("content.php", $data);
$this->load->view("footer.php", $load_js);

footer.php:

   // start of footer stuff here
   $this->load->view($load_js);
</body>
</html>

page1.php:

<script>
   // Your scripts here
</script>

:

$this->load->view("header.php");
$this->load->view("content.php", $data);

content.php:

 // Content goes here
$data['load_js'] = "page1.php";
$this->load->view("footer.php", $data);

"footer.php":

   // start of footer stuff here
   $this->load->view($load_js);
</body>
</html>

page1.php:

<script>
   // Your scripts here
</script>

, , , . , , , , , , js.

, $load_js js .

+2

, , . , .

, , :

<html>
  <head>
    <title><?php echo $page->title ?></title>
    <?php if (count($page->css)): ?>
       <?php for ($i=0; $i < count($page->css); $i++): ?>
         <link rel="stylesheet" href="<?php echo $page->css[$i] ?>"/>
       <?php endfor; ?>
    <?php endif; ?>
  </head>
  <body>
    <?php $this->load->view('header.php'); ?>

    <?php $this->load->view($content); ?>        

    <?php $this->load->view('footer.php'); ?>

    <?php if (count($page->js)): ?>
      <?php for ($i=0; $i < count($page->js); $i++): ?>
        <script src="<?php echo $page->js[$i] ?>"></script>
      <?php endfor; ?>
    <?php endif; ?>
  </body>
</html>

$page, css js. , , global.js, , ( CSS ). , . $page ( global.js). / global.js, .

$content .

, HTML (, 1 , 2 , 3 ). .. .

, JS , JS , , JS. , , JS .

+1

; , global.js script. , global.js , .

0

- .

Williams.
JS ( CSS) /.
:

class foo exends Controller {
    public function bar() {
        $this->template->add_js('js/jquery.js');
        $this->template->add_js('alert("Hello!");', 'embed');

        $data = $this->some_model->get_data();
        $this->template->write_view('content', 'user/profile', $data;

        $this->template->render();
    }
}

, JS , .
global.js , , JS.

0

/

public function add()
    {
        $data['page_title'] = 'Test | Add Details';
        $data['js'] = array('details','details2');

        $this->load->view('template/header',$data);
        $this->load->view('details/add');
        $this->load->view('template/footer',$data);
    }

, , jquery.min.js, bootstrap.min.js ..

 <!-- Jquery Core Js -->
    <script src="<?= base_url() ?>assets/plugins/jquery/jquery.min.js"></script>

    <!-- Bootstrap Core Js -->
    <script src="<?= base_url() ?>assets/plugins/bootstrap/js/bootstrap.js"></script>   

    <?php 
    if(isset($js) && count($js) > 0)
    {
        for($i=0;$i<count($js);$i++)
        {

    ?>
        <script src="<?= base_url() ?>assets/js/custom/<?= $js[$i] ?>.js"></script>
    <?php
        }
    } 
    ?>
</body>
</html>

js, .

0

If you have a footer.php file that is included on every page, why can't you just put your js code in this file?

-1
source

All Articles