Michael Grigsby
Michael Grigsby

Reputation: 12163

CodeIgniter not calling a js file

In CodeIgniter I've got a view file setup in the root folder, then I've got some Javascript files in a js folder as a sub-folder in the view. I tried doing simply:

<script src="js/jquery-blink.js" language="javscript" type="text/javascript"></script>

But this doesn't seem to work. So is there a specific inclusion technique I need to be performing in order to include my external view requirement files?

Upvotes: 1

Views: 2996

Answers (3)

Josnidhin
Josnidhin

Reputation: 12504

Try the following.

Set base url in the your config file

$config['base_url'] = 'your website url here'

Add base tag in your html head.

<base href="<?php echo base_url(); ?>" />

<script src="js/jquery-blink.js" language="javascript" type="text/javascript"></script>

If still not working check your .htaccess file

Upvotes: 0

Repox
Repox

Reputation: 15476

Try doing a call to the absolute path:

<script src="/js/jquery-blink.js" language="javascript" type="text/javascript"></script>

When going to a controller other than the default one, the browser think you are inside a subfolder.

If you go to http://ecample.org/index.php/controller/ then with your current script link, your browser will think you are looking the JS file in /controller/js/jquery-blink.js

But adding the slash infront of the src path, makes it look in the absolute path.

Upvotes: 1

Seralize
Seralize

Reputation: 1127

By using the URL-helper you can insert a dynamic base URL which you set in the application/config/config.php:

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

Now, whenever you call the base_url(); function, CodeIgniter will replace it with the set base URL:

<script src="<?php echo base_url(); ?>js/jquery-blink.js" language="javascript" type="text/javascript"></script>

This line will then become:

<script src="http://localhost/ci/js/jquery-blink.js" language="javascript" type="text/javascript"></script>

Remember that you need to load the helper like this $this->load->helper('url'); or add it in the 'helpers' array in application/config/autoload.php

Upvotes: 2

Related Questions