3
votes

First of all, I am sorry for my bad english, I am spanish and I am sorry if I have made something wrong, this is my first post.

I have a problem with loading a javascript file in codeigniter 2.1.4. I didn´t get loaded it in the header of my page. The css files were well loaded. This example attempts to load jquery and bootstrap.

I am following the steps in the documentation of codeigniter and when I create the codeigniter static page "home.php" I want to use bootstrap for design.

The tutorial I followed is this: http://ellislab.com/codeigniter/user-guide/tutorial/static_pages.html

My folder structure :

  • htdocs
    • myproject (curriculos)
      • application
        • controller
          • pages.php
        • views
          • templates
            • header.php
          • pages
            • home.php
            • about.php
      • system
      • assets
        • bootstrap
          • css
            • bootstrap.min.css
          • js
            • bootstrap.min.js
        • jquery- 2.1.0.min.js

I have been searching in this forum and others and always I find the same example with base_url() or site_url(). These are mine:

Updated code:

<script type="text/javascript" src="<?php echo base_url(); ?>assets/bootstrap/js/bootstrap.min.js"></script>

My autoload.php with url helper loaded:

$ autoload['helper'] = array('url');

When I try to load the page I get these errors:

GET http://localhost/curriculos/assets/jquery-2.1.0.min.js 404 (Not Found)
GET http://localhost/curriculos/assets/bootstrap/js/bootstrap.min.js 404 (Not Found)
GET http://localhost/curriculos/assets/bootstrap/js/bootstrap.min.js 404 (Not Found) 

The strangest is that when you click on one of them it shows you that the file type is trying to be loaded as a text/html and not as an application/javascript as usual:

Error loading: file type text/html

Can you help me?

Thank you very much for your reply.

4
are the images working fine or the same is the problem with the images?Guns
I uploaded a new image with new suggestions.Is this your question? Maybe I didn't understand you well.mrroot5
Sorry for my english, now I think that I understand you. Yes, the image is correct for the new updated code. Sorry, my text editor teased me when I copied my question from the editor to stack overflow.mrroot5
I found the problem. The problem is the .htaccess from codeigniter to hide index.php in url ¬¬. This is my .htaccess: RewriteEngine on RewriteCond $1 !^(index\.php|images|robots\.txt) RewriteRule ^(.*)$ index.php/$1 [L] Someone knows how to change the .htaccess to admit .js files? @Guns @Jai @shan22mrroot5

4 Answers

5
votes

You need to change your .htaccess file so that it no longer rewrites requests made to '/assets'.

In other words, you need your .htaccess file to ignore requests to the 'assets' folder and just let the browser load the url as provided. Try this:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
0
votes
<script  src="<?php echo base_url(); ?>assets/bootstrap/js/bootstrap.min.js"></script>

This is the correct path.Try it!

0
votes

The problem is the .htaccess provided by codeigniter to remove the index.php. Follow this link: http://www.codeigniter.com/userguide2/general/urls.html

Remove this code in your .htaccess and you can load javascript files :).

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
0
votes

Assets folder must be outside the application folder. Like below snapshot. Then user the below code in views files to include css, js and fonts files.

Folder structure for css/js/fonts files in codeigniter

<link href="<?php echo base_url('assets/css/bootstrap.min.css'); ?>" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<?php echo base_url("assets/js/jquery-1.11.3.min.js"); ?>"></script>
<script type="text/javascript" src="<?php echo base_url("assets/js/bootstrap.js"); ?>"></script>