jstandshigh
jstandshigh

Reputation: 109

How to add external css and javascript file in CodeIgniter

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:

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)

Upvotes: 1

Views: 675

Answers (1)

user4419336
user4419336

Reputation:

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>

Upvotes: 3

Related Questions