jquery javascript doesnt work in codeigniter

前端 未结 3 1075
孤独总比滥情好
孤独总比滥情好 2021-01-26 08:41

Hi everybody and happy thanksgivingday !

i am some problem to include this script in codeigniter:



        
相关标签:
3条回答
  • 2021-01-26 09:07

    An alternative way is as follows.

    <script type="text/javascript" src="<?= base_url('application/views/jquery.js'); ?>">
    

    In this case $config['base_url'] has a trailing /

    If you are trying to access files directly under applications/views you will get a 404 error. Is that your issue?

    Normally you would put all your public side css and js files under a structure like...

    Your Document Root Folder/
                             assets/
                                   js/
                                      jquery.js
                                   css/
    

    So then you would have

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

    And then you can access it correctly.

    The trick is... view your HTML source code on the browser, view the link you have for your jquery and click on it and see what the nice error message says.

    if it all works, then you will see the actual file source.

    0 讨论(0)
  • 2021-01-26 09:13

    seems your src is incorrect

    <script type="text/javascript" src="<?php echo base_url();?    application/views/jquery.js">
    

    try to change it to

    <script type="text/javascript" src="<?php echo base_url(); ?>"application/views/jquery.js">
    
    0 讨论(0)
  • 2021-01-26 09:15

    First of all you cannot directly call any files which is inside of application folder. Create a new folder js in your home directory and move your jquery.js into that i.e., root_folder -> js -> jquery.js Then try this example

    Controller

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Test extends CI_Controller {
    
        public function __construct ()
        {
            parent::__construct();
        }
    
        public function index()
        {
                $this->load->view('test');
        }
    
        public function hello()
        {
                $this->load->view('data');
        }
    }
    

    View ( test.php )

    <!DOCTYPE html>
    <html>
    <head>
        <title>test project</title>
    </head>
    <body>
    <div id="show"> This content will replaced by your interval response </div>
    
    <script src="<?php echo base_url(); ?>js/jquery.js"></script>
    <script type="text/javascript">
    
    $(document).ready(function () {
        setInterval(function () {
                $('#show').load('<?php echo base_url(); ?>test/hello');
                console.log('test');
            }, 3000);
    });
    
    </script>
    
    </body>
    </html>
    

    2nd view file ( data.php )

    <div class="hello">
        <p>Hello world</p>
    </div>
    
    0 讨论(0)
提交回复
热议问题