chris
chris

Reputation: 211

Link Javascript in codeigniter

I am using php web framework codeignter, I would want to access my link on Javascript, I've tried

<script type="text/javascript" src="<?php echo "assets/main.js;?>"></script>.

Autoload url helper is enabled but it doesn't load my Javascript.

Thanks!

Upvotes: 0

Views: 362

Answers (1)

Mohammed Sufian
Mohammed Sufian

Reputation: 1781

simple and clean way is here:

create a helper class:

create a utility_helper.php class in helper folder and only put the below code in the utility_helper.php

code to put: (with <?php ...below code here... ?>)

function asset_url(){
  return base_url().'assets/';
}

open your autoload.php and make sure you include utility in autoload['helper'] array:

$autoload['helper'] = array('url','utility','file','form','etc...');

important: now create assets folder at the same location where you have your system and application folders.. and put your all css and js files and folder in assets folder

now you can link your css and js in view like below:

<script src="<?php echo asset_url(); ?>js/jquery.js"></script>
<link href="<?php echo asset_url(); ?>css/style.css"/>
<link rel="icon" href="<?php echo asset_url(); ?>img/favicon.ico" type="image/x-icon"/>

GOOD LUCK :)

Upvotes: 3

Related Questions