Welcome Guest, Not a member yet? Register   Sign In
Including Javascript files
#1

[eluser]Alhazred[/eluser]
I've already read many threads about this, but no one solved my problem.

I've put my js file into a folder named js into the root, at the same level of CI index.php

Inside the view where I want to use the js file I've put
[script type='text/javascript' src='<?php echo base_url() ?>js/check.js' ][/script]
I've used the proper < and > in place of [ and ] in my code.

looking at the page source code the src is correct: http://localhost/js/check.js but the code doesn't work.
If I click on the link shown in the page's source code I get an error for file not found.

May that depend on the htaccess file and rewrite rules?
I've used htaccess to remove the index.php from the url.

If htaccess is not a problem, what should I try to fix this problem?
#2

[eluser]CroNiX[/eluser]
depends on your htaccess rules, which you should post. Most likely you aren't allowing access if you are using one of the htaccess files floating around the forums and haven't adjusted it properly. Use the one with -F and -D rules, not the other which needs constant adjustment whenever you add files/directories, which is stupid (you'd have to add your js dir to it to allow access).

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
#3

[eluser]Sorry[/eluser]
just put this in your .htaccess

Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|favicon\.ico|robots\.txt|js)
RewriteRule ^(.*)$ /index.php/$1 [L]

instead of
Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

there it is also possible to access the favicon.ico, so you just have to put one in your root directory

-> oh i was too slow...
#4

[eluser]CroNiX[/eluser]
That's the lame one I was referring to that needs constant adjustment.
#5

[eluser]Sorry[/eluser]
[quote author="CroNiX" date="1341853296"]That's the lame one I was referring to that needs constant adjustment.[/quote]ok, your rule is much better, maybe it should be added in the documentation.
#6

[eluser]CroNiX[/eluser]
It has been for CI3
https://github.com/EllisLab/CodeIgniter/...l/urls.rst
#7

[eluser]Alhazred[/eluser]
[quote author="CroNiX" date="1341852784"]
Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
[/quote]
Thanks, this solved the problem

My htaccess was
Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
#8

[eluser]Sorry[/eluser]
yeah, it has to be changed here:
http://ellislab.com/codeigniter/user-gui.../urls.html
#9

[eluser]Near[/eluser]
are you sure this link is correct? http://localhost/js/check.js

should it be http://localhost/application/js/check.js
#10

[eluser]Alhazred[/eluser]
[quote author="Near" date="1341916102"]are you sure this link is correct? http://localhost/js/check.js

should it be http://localhost/application/js/check.js[/quote]
Yes, I'm sure, as suggested all over the websites I've checked, I put my static contents such as javascript, css and image inside my site's root, so at the same level as index.php, so the first link is correct.
By the way, I've solved the problem changing my htaccess as said above.




Theme © iAndrew 2016 - Forum software by © MyBB