1

I am facing problem adding external css and javascript file in my view file. I have created separate directory for CSS and Javascript. Currently my directory looks like:

  • config
  • controller
    • AdminTabs.php
  • CSS
    • tab_v.css
  • Javascript
    • tab_v.js
  • models
  • views
    • admin
      • tab_v.php

I have included the files in view tab_v.php like this

<link rel="stylesheet" type="text/css" href="../CSS/tab_v.css">
<script type='text/javascript' src="../Javascript/tab_v.js"></script>

tab_v.php is loaded by the controller AdminTabs.php I get error:

  domainname/app/CSS/tab_v.css Failed to load resource: the server responded with a status of 404 (Not Found)
  domainname/app/JavaScript/tab_v.js Failed to load resource: the server responded with a status of 404 (Not Found)
3
  • You should create a assets folder out side of application folder. Commented Jan 13, 2017 at 0:10
  • Can you show an example how the path will look like if I create a folder? Commented Jan 13, 2017 at 0:16
  • Added answer should help Commented Jan 13, 2017 at 0:26

1 Answer 1

3

First load the url helper application > config > autoload.php

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

And then set your base url in application > config > config.php if not css links and js may not work

$config['base_url'] = 'http://localhost/projectname/';

if on hosting url look like something

$config['base_url'] = 'http://www.example.com/';

Directory

application

assets

assets > css > tab_v.css

assets > js > tab_v.js

system

index.php

Head

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/css/tab_v.css');?>">
<script type='text/javascript' src="<?php echo base_url('assets/js/tab_v.js');?>"></script>
</head>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer

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.