日期:2014-05-16  浏览次数:20518 次

如何在CodeIgniter中引入外部的JS和CSS文件

1.设置重定向

修改apache配置文件httpd.conf
1、//开启rewrite
LoadModule rewrite_module modules/mod_rewrite.so
2、//开启 .htaccess,找到网站根目录<Directory "E:/PHPWEB">修改:
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
然后,在 CI 根目录下新建立一个配置文件,命名为: .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\\.php|images|robots\\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
在里面这样写:就可以去掉 index.php 了。要注意 /index.php/$1 要根据你目录(Web 目录,比如 http://www.domain.com/index.php)的实际情况来定,比如网站根目录是 /ci/index.php 则要写成 /ci/index.php/$1

2.把JS,CSS,IMG等资源文件夹与SYSTEM文件夹放在同一级下,然后在JS文件夹中建立js文件,例如:ajax.js。

3.在application\config\config.php文件中设置base_url :
$config['base_url'] =http://www.exiplode/com; //这里是你的网站根目录
4.在controller构造方法中调用如下代码:
$this->load->helper('url')
5.在VIEW层中具体页面中引入即可:
<script type="text/javascript" src='<?=base_url().'js/Ajax.js'?>'></script>
6.可以使用html的<base href=''<?=base_url()?>"来简化:
<script type="text/javascript" src='js/Ajax.js'></script> 这一点在导入图片时非常有用.