php CodeIgniter:如何在视图中包含 .js 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1543407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter: How do I include a .js file in view?
提问by Poku
Where and how do I include .js files in Views in CodeIgniter?
我如何在 CodeIgniter 的视图中包含 .js 文件?
I have tried this:
我试过这个:
<script type="text/javascript" src="system/application/libraries/jquery.js"></script>
As I figured that the index.php is the one that is loading the views, so I'm guessing that every time a page is loaded the current dir path is at root because index.php is located at root. Is this true?
因为我认为 index.php 是加载视图的那个,所以我猜测每次加载页面时,当前目录路径都在根目录下,因为 index.php 位于根目录下。这是真的?
The above line doesn't so I am doing something wrong. I have a CodeIgniter project. The path is like this:
上面的行没有,所以我做错了什么。我有一个 CodeIgniter 项目。路径是这样的:
localhost/CodeIgniter/system/application
so which path should I use to include my jquery.js file which is located in
所以我应该使用哪个路径来包含我的 jquery.js 文件,它位于
localhost/CodeIgniter/system/application/libraries
when I want to load the jquery.js file in a View located here:
当我想在位于此处的视图中加载 jquery.js 文件时:
localhost/codeIgniter/system/application/views/till_view.php
采纳答案by ChronoFish
It's not the "index.php" file that is the view. The view is whatever is loaded in your controller when you do a
视图不是“index.php”文件。视图是您执行以下操作时在控制器中加载的任何内容
$this->load->view("viewname");
$this->load->view("viewname");
The file "viewname.php" can then include the .jsfiles, just as a normal .html(or .php) file would:
然后文件“ viewname.php”可以包含.js文件,就像普通的.html(或.php)文件一样:
<script src="/url/to/javascript.js" />
You may want to create a default view or a "header" view that includes some or all of the (common) .jsfiles for your project.
您可能希望创建一个默认视图或“标题”视图,其中包含您项目的部分或全部(常见).js文件。
-CF
-CF
回答by Phil Sturgeon
There is a lesser-known solution that works really well with clean syntax and much more portability than hard-coding URL's or relative files.
有一个鲜为人知的解决方案,与硬编码 URL 或相关文件相比,它具有清晰的语法和更多的可移植性。
<base href="<?=base_url();?>">
<img src="images/logo.gif" alt="Logo" />
Read more about how, what, why on my article "Asset handling in CodeIgniter with the BASE tag".
在我的文章“带有 BASE 标签的 CodeIgniter 中的资产处理”中阅读更多关于如何、什么、为什么的信息。
回答by Ikke
First question:
第一个问题:
It depends if you use absolute or relative urls.
这取决于您使用绝对网址还是相对网址。
Absolute urls go from the root of your domain. Relative urls are loaded relative from the current directory (including the url segments).
绝对网址来自您域的根目录。相对 url 是从当前目录(包括 url 段)相对加载的。
Second question: It's best to use an absolute URL. Because of the pretty urls, it's not recommended to use relative urls.
第二个问题:最好使用绝对 URL。由于网址比较漂亮,不推荐使用相对网址。
The easiest way is to use the url helperand then use the site url function like this:
最简单的方法是使用url helper,然后像这样使用站点 url 函数:
$this->load->helper('url');
echo site_url('system/application/libraries/jquery.js');
Ps. I recommend to put things like javascript and images outside of the CodeIgniter directory.
附言。我建议将诸如 javascript 和图像之类的内容放在 CodeIgniter 目录之外。
回答by stef
base_url()always works fine for me
base_url()对我来说总是很好
回答by wired00
Here is a solution specifically relevant to the OP which I didn't think anyone else provided.
这是一个与 OP 特别相关的解决方案,我认为其他人没有提供。
<script type="text/javascript" src="<?= base_url() ?>libraries/jquery.js"></script>
So, base_url()will provide a path direct to the root of your site.
因此,base_url()将提供一个直接到您网站根目录的路径。
回答by Nooha Haris
<script type="text/javascript" src="<?php base_url() ?>libraries/jquery.js"></script>
base_url()will provide the url of the site
base_url()将提供网站的网址
回答by Muhammad Hasan
The easiest way you can directly access the file:
您可以直接访问文件的最简单方法:
<img src="http://localhost/ci/you_dir/your_img/your_image.png" />
where ci is for codeigniter.
其中 ci 用于 codeigniter。

