Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 392
Poku Avatar asked Oct 09 '09 12:10

Poku


People also ask

Where do I put JavaScript in CodeIgniter?

Adding JavaScript and CSS (Cascading Style Sheet) file in CodeIgniter is very simple. You have to create JS and CSS folder in root directory and copy all the . js files in JS folder and . css files in CSS folder as shown in the figure.

How do I embed a JavaScript file?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Which is the right place to include a JavaScript file?

The scripts should be included at the end of the body tag because this way the HTML will be parsed by the browser and displayed before the scripts are loaded.


1 Answers

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".

like image 163
Phil Sturgeon Avatar answered Oct 05 '22 23:10

Phil Sturgeon