How to introduce external JS and CSS in CI?

Source: Internet
Author: User

In fact, both CI and ZF have the same problem, that is, the path problem. In the early stage, when I was using ZF for CMS, I set in the. htaccess file that no redirection was required for js, css, img, and other resource files. However, when I was using CI today, I forgot that I had never been involved for a long time. I finally decided on the official Chinese website of CI with their help, post it here for your sharing.

(Another note: I have hidden the index. php file in the url. It is different from not hiding, but the absolute URL is used in the end)

First, set in the. htaccess file (to hide index. php) as follows:

RewriteEngine on
RewriteCond $1! ^ (Index/. php | images | js | img | css | robots/. txt) # Write the resources to be excluded here.
RewriteRule ^ (. *) $ index. php/$1 [L]

. In this way, when I want to introduce JS, the following code is displayed:

<Script type = "text/javascript" src = "url/js/ajax. js'?> "> </Script>: note that the url here is the absolute URL of your website (the absolute URL is http: // domain/js/ajax. js, which is called the absolute URL)

That's all. For maintainability, you can also use the BASE_URL of CI, as shown below:

Go to the application/config. php file and set base_url In the config. php file:

$ Config ['base _ url'] = http://www.exiplode/com; // the root directory of your website

After the settings are complete, go to the controller.

$ This-> load-> helper ('url '),

Then you can introduce it to the specific page of the VIEW layer:
<Script type = "text/javascript" src = '<? = Base_url (). 'js/Ajax. js'?> '> </Script>

Okay, it's so easy to introduce external JS and CSS in CI.

Note: "RewriteCond $1! ^ (Index /. php | images | js | img | css | robots /. txt) "The code here means that any resource you want to access will not be redirected and can be written here. Sometimes, when the website does not load CSS or JS (its path is correct), it is redirected.

If you have any questions, please contact me or go to CI's China official forum http://codeigniter.org.cn/forums/forum-4-1.html. I have discussed this a lot.

(In addition, thanks to the Hex and visvoy on the CI China official forum)

For your security, please only open the URL with reliable source

Open website {
Share. safelink. close (); return false;
} "Href =" http://writeblog.csdn.net/# "> cancel

From: http://hi.baidu.com/fc%5Flamp/blog/item/1e8bab1f2f2d5efd1bd576ef.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.