0

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!

8
  • <script src="<?php echo base_url();?>/jquery/1.7.1/jquery.min.js"></script> Commented Mar 11, 2014 at 6:57
  • <script type="text/javascript" src="<?php echo base_url("assets/main.js");?>" </script> Commented Mar 11, 2014 at 7:10
  • @kumar i've loaded my css using the code above, will this work with Javascript? Commented Mar 11, 2014 at 7:11
  • yes. Give the correct path to js file. Commented Mar 11, 2014 at 7:12
  • @chris if Your above code work with php then it will 100% work with javascript. first thing you make sure that "assets" is properly defined or not. Commented Mar 11, 2014 at 7:14

1 Answer 1

3

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 :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.