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 do not want to use the template library, follow these steps:
* provided that on the controller "Map", and you need a 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>'); $data['scripts'] = $scripts; $this->load->view('my_map/index', $data); } }
In your opinion:
if(isset($scripts)) { foreach($scripts as $script) { echo $script; } }
essentially, you create an array of files "> w370> files / css (independently), then check for its presence and unload it in the head section of your view.
I personally would choose a template.
Also note that CI2.0 has a new javascript driver , it might be worth a read
source share