Codeigniter - Load a specific JS library on a specific view

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.

Answers:

Answer

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.

Answer

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

Answer
<?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');
Answer

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!

Answer

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.