Poku
Poku

Reputation: 3178

CodeIgniter: How do I include a .js file in view?

Where and how do I include .js files in Views in CodeIgniter?

I have tried this:

<script type="text/javascript" src="system/application/libraries/jquery.js"></script>

As I figured that the index.php is the one that is loading the views, so I'm guessing that every time a page is loaded the current dir path is at root because index.php is located at root. Is this true?

The above line doesn't so I am doing something wrong. I have a CodeIgniter project. The path is like this:

localhost/CodeIgniter/system/application

so which path should I use to include my jquery.js file which is located in

localhost/CodeIgniter/system/application/libraries

when I want to load the jquery.js file in a View located here:

localhost/codeIgniter/system/application/views/till_view.php

Upvotes: 7

Views: 51368

Answers (7)

Nooha Haris
Nooha Haris

Reputation: 41

<script type="text/javascript" src="<?php base_url() ?>libraries/jquery.js"></script>

base_url() will provide the url of the site

Upvotes: 0

stef
stef

Reputation: 27749

base_url() always works fine for me

Upvotes: 4

wired00
wired00

Reputation: 14438

Here is a solution specifically relevant to the OP which I didn't think anyone else provided.

<script type="text/javascript" src="<?= base_url() ?>libraries/jquery.js"></script>

So, base_url() will provide a path direct to the root of your site.

Upvotes: 0

Muhammad Hasan
Muhammad Hasan

Reputation: 9

The easiest way you can directly access the file:

<img src="http://localhost/ci/you_dir/your_img/your_image.png" />

where ci is for codeigniter.

Upvotes: -2

ChronoFish
ChronoFish

Reputation: 3707

It's not the "index.php" file that is the view. The view is whatever is loaded in your controller when you do a

$this->load->view("viewname");

The file "viewname.php" can then include the .js files, just as a normal .html (or .php) file would:

<script  src="/url/to/javascript.js" />

You may want to create a default view or a "header" view that includes some or all of the (common) .js files for your project.

-CF

Upvotes: 7

Phil Sturgeon
Phil Sturgeon

Reputation: 30766

There is a lesser-known solution that works really well with clean syntax and much more portability than hard-coding URL's or relative files.

<base href="<?=base_url();?>">

<img src="images/logo.gif" alt="Logo" />

Read more about how, what, why on my article "Asset handling in CodeIgniter with the BASE tag".

Upvotes: 14

Ikke
Ikke

Reputation: 101231

First question:

It depends if you use absolute or relative urls.

Absolute urls go from the root of your domain. Relative urls are loaded relative from the current directory (including the url segments).

Second question: It's best to use an absolute URL. Because of the pretty urls, it's not recommended to use relative urls.

The easiest way is to use the url helper and then use the site url function like this:

$this->load->helper('url');
echo site_url('system/application/libraries/jquery.js');

Ps. I recommend to put things like javascript and images outside of the CodeIgniter directory.

Upvotes: 6

Related Questions