1

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?

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

<script src="<?php echo base_url(); ?>application/views/js/jquery-blink.js" language="javscript" type="text/javascript"></script> Your methodology doesn't seem to work. But to add application/views/ after echoing out the base_url() inside the view seems to work just fine. Is this good practice though?
If I was you I would separate CSS and Javascript from your view folder. I like to keep a public folder outside the CodeIgniter folder, so that public/ will be relative to the index.php file. That's why I used that example. After all, when fetching resources you are not referencing the views, you are references the controller (which calls the views).
1

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.

2 Comments

Because you taught me something. That counts doesn't it? Lol
@MichaelGrigsby, you should only mark this as answer if it SOLVES your problem, otherwise other people will find this question and be confused why its the incorrect answer.
0

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

Comments

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.