0

I have read all the topics "Including Javascript files in CodeIgniter" but these don't work for me! I reduced my problem to the following code.

I work with WAMP and I copied Codeigniter in c:\wamp\www\codeigniter folder.

In c:\wamp\www\codeigniter\application\controlers\ I have a file mnd3c_test.php that contains:

<?php
class mnd3c_test extends CI_Controller {
public function __construct()
  {parent::__construct();}
public function test()
  {$this->load->view('mnd3v/mnd3v_test.php'); }
}

In c:\wamp\www\codeigniter\application\view\mnd3v\ I have a file mnd3v_test.php that contains:

<html>
<head>
<script type="text/javascript" src="mnd3js.js" ></script>
</head>
<body>
<h3 onclick="fsort()">Click on me to see alert message</h3>
</body>
</html>

In c:\wamp\www\codeigniter\application\view\mnd3v\ I also have a file mnd3js.js that contains:

function fsort()
{  alert ("fsort"); }

I have set in config.php:

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

I load my page in Internet Explorer or Google Chrome with:

http://localhost/codeigniter/index.php/mnd3c_test/test 

and when I click on "Click on me to see alert message" nothing happens (In IE I have a "Error on page" message). I have tried to put in src atribute all kind of values, like mnd3v/mnd3js.js or localhost/application/views/mnd3v/mnd3js.js but nothing happend.

I would like to see the alert message. Can anybody help me?

16
  • The path to your JavaScript file is wrong. Where is your index.php file located? If your src URL is simply mnd3js.js, then it would need to be located in same directory as your index.php. Commented Jan 20, 2015 at 19:42
  • index.php file is located in codeigniter folder. I put mnd3js.js in codeigniter folder but nothing happen. In fact I put mnd3js.js in many folders, but I didn't see the alert message. Commented Jan 20, 2015 at 19:50
  • All I can tell you is if your src path is simply mnd3js.js, then it would need to be located in same directory as your index.php. What troubleshooting have you performed? Have you inspected your JavaScript console errors? Are you sure you spelled the file name correctly? Commented Jan 20, 2015 at 19:51
  • "I put mnd3js.js in many folders" ~ If you insist on programming by "trial & error" rather than by logic & knowledge, you might as well copy the file into all folders at once. If it works, delete them one by one until it breaks again. Hopefully, you'll learn something along the way. Commented Jan 20, 2015 at 19:59
  • The name is ok. I don't know how to inspect my code with JavaScript console. Commented Jan 20, 2015 at 20:01

3 Answers 3

2

You don't need to include the javascript file into the views folder. In fact I would say this is actually a bad idea, since the view folder in Codeigniter is meant to hold php files which are kind of like a templates.

Instead consider putting javascript or other loadable media in their own folder outside of Codeigniter. Simply create a folder called js off of your root (it's probably the same level as the application folder). Then just reference the files using relative links

<script type="text/javascript" src="/js/mnd3js.js" ></script>

or with an absolute link

<script src="<?php echo base_url() ?>js/jquery.js"></script>

I prefer relative links myself, but ultimately it depends on your application.

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

2 Comments

Unless you have compelling reason not to, you should always err on the side of including the base_url.
I wanted to put my JavaScript file inside the codeigniter application folder because this code was developped for this aplication, and only the javascript code bring from other sources, such JQuery for example, is appropriate to stay outside the application folder. But this isn't a problem, I can put anywhere my JavaScript code if it works!
0

I do this way <script src="<?php echo base_url() ?>js/jquery.js"></script> where js source exist in my public path

1 Comment

Thank you, your answer help me to discover the source of my problems!
-1

Using WAMP, CodeIgniter, JavaScript, if you need to include JavaScript file in a view file with src attribute, you have to put js file outside the application folder. If the src address points to a subfolder of codeigniter application folder, CodeIgniter doesn't know to interpret this address. Why, I don't know.

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.