Codeigniter - Load a specific JS library on a spec

2019-02-11 06:28发布

问题:

I'm trying to load the google maps API ie:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true">

in my head template. But because I've only got one page with a google map on it (I'd rather not have the API load for all files), how would I send the message from the controller through to the view that I wish to load this particular JS file?

Thanks for your help.

回答1:

CodeIgniter has a segments class. You would be able to run some code like:

<?php if($this->uri->segment(1) == 'map') { ?>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true">
<?php } ?>

When on page http://yoursite.com/map/ it will load the script.



回答2:

One solution is to either use a template library that has javascript/css "injection" - see:

http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html#utilities

$this->template->add_js('js/jquery.js');
$this->template->add_js('alert("Hello!");', 'embed');

for more information.

If you don't want to use a template library, do something like this:

*assuming on the "Map" controller, and that you need the JS file on the default page.

class Map extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $scripts = array(
    '<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true">' . "\n", 
    '<script>something</script>');
       /* this method lets you add multiple...*/

        $data['scripts'] = $scripts;
        $this->load->view('my_map/index', $data);
    }
}

in your view:

if(isset($scripts))
{
    foreach($scripts as $script)
    {
        echo $script;
    }
}

essentially you build an array of script files/css files (whatever), then check for its prescence and dump it in the head section of your view.

I'd personally go for the template option.

Also note CI2.0 has a new javascript driver might be worth a read



回答3:

<?php

/**
 * Head files loader
 * @author azhar
 **/

function headscripts($path)
{
    if(is_string($path))
    {
        echo "<script type='text/javascript' src='". base_url($path) ."'></script>\n";
    }elseif(is_array ($path)){
        foreach ($path as $p) {
            echo "<script type='text/javascript' src='". base_url($p) ."'></script>\n";
        }
    }
}

function headlinks($path)
{
    if(is_string($path))
    {
        echo "<link rel='stylesheet' href='". base_url($path) ."'/>\n";
    }elseif(is_array ($path)){
        foreach ($path as $p) {
            echo "<link rel='stylesheet' href='". base_url($p) ."'/>\n";
        }
    }
}
?>

Add this file in your helper_directory under the name head_helper. In your controller inside an action use this code

$data['headscripts'] = array('js/main.js');

And in your view file use this function

headscripts($headscripts);

For stylesheet use this

headlinks($headlinks);

And yes do not forget to load the helper using autoload.php file in config folder like this

$autoload['helper'] = array('url', 'file','head');


回答4:

Thanks for your answers guys, I ended up doing a mixture of Ross and leaf dev's suggestions before I found your answers here, so I guess I was on the right track.

My controller has:

$data['head'] = array('specificjs');
$this->load->view('view',$data);`

and my view has:

if(isset($head)){
    foreach($head as $item){
        $this->load->view('js/'.$item);
    }
}

and my 'specificjs' view has what's required.

This way I can load as many custom scripts as I want and have the code in a view not a controller.

Thanks again, but keep any further suggestions coming!



回答5:

write a helper for this. Pass the scripts names in an array and inside the helper function iterate over them and print the scripts