Codeigniter:如何包含 javascript 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3841286/
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 to include javascript files
提问by Raj
Hello I just started working with CodeIgniter framework. My current directory structure is
您好,我刚开始使用 CodeIgniter 框架。我当前的目录结构是
Demo(Project name)
+System
+Application
-Controllers
demo.php
+Model
-Views
view_demo.php
-Js
ajax.js
jquery.js
Please tell me how to include .js files in view_demo.php.
请告诉我如何在 view_demo.php 中包含 .js 文件。
Thanks Raj
谢谢拉吉
回答by ipalaus
You need to use the base_url()
to include the javascript file in your VIEW.
您需要使用 将base_url()
javascript 文件包含在您的 VIEW 中。
So, in the view_demo.php file:
因此,在 view_demo.php 文件中:
<script type="text/javascript" src="<?=base_url()?>js/jquery.js" ></script>
<script type="text/javascript" src="<?=base_url()?>js/ajax.js" ></script>
You will need the URL helper loaded. To load the helper you need to put on your demo.php controller:
您将需要加载 URL 帮助程序。要加载助手,您需要将其放在 demo.php 控制器上:
$this->load->helper('url');
You can also autoload on \config\autoload.php on the helpers array.
您还可以在 helpers 数组上的 \config\autoload.php 上自动加载。
More info about base_url(): http://www.codeigniter.com/user_guide/helpers/url_helper.html#base_urlhttps://codeigniter.com/user_guide/general/styleguide.html#short-open-tags
有关 base_url() 的更多信息:http: //www.codeigniter.com/user_guide/helpers/url_helper.html#base_url https://codeigniter.com/user_guide/general/styleguide.html#short-open-tags
回答by RC.
You wouldn't include JS files within the PHP, they would be output as script tags within the HTML you produce which you may be producing as output from the PHP script.
您不会在 PHP 中包含 JS 文件,它们将在您生成的 HTML 中作为脚本标签输出,您可能会作为 PHP 脚本的输出生成这些标签。
As far as I know, there is no built in CodeIginiter function to include this output like there is for CSS using the link_tag()
function provided by CI. I've added a function called script_tag()
to the system/helpers/html_helper.php
file from CI. The function is:
据我所知,没有内置的 CodeIgniter 函数来包含这个输出,就像使用link_tag()
CI 提供的函数的CSS 一样。我添加了一个调用的函数script_tag()
的system/helpers/html_helper.php
从CI文件。功能是:
if ( ! function_exists('script_tag')) {
function script_tag($src = '', $language = 'javascript', $type = 'text/javascript', $index_page = FALSE)
{
$CI =& get_instance();
$script = '<scr'.'ipt';
if (is_array($src)) {
foreach ($src as $k=>$v) {
if ($k == 'src' AND strpos($v, '://') === FALSE) {
if ($index_page === TRUE) {
$script .= ' src="'.$CI->config->site_url($v).'"';
}
else {
$script .= ' src="'.$CI->config->slash_item('base_url').$v.'"';
}
}
else {
$script .= "$k=\"$v\"";
}
}
$script .= "></scr"."ipt>\n";
}
else {
if ( strpos($src, '://') !== FALSE) {
$script .= ' src="'.$src.'" ';
}
elseif ($index_page === TRUE) {
$script .= ' src="'.$CI->config->site_url($src).'" ';
}
else {
$script .= ' src="'.$CI->config->slash_item('base_url').$src.'" ';
}
$script .= 'language="'.$language.'" type="'.$type.'"';
$script .= ' /></scr'.'ipt>'."\n";
}
return $script;
}
}
Then in your PHP code you can do:
然后在您的 PHP 代码中,您可以执行以下操作:
echo script_tag('content/js/jquery-1.4.2.js');
回答by kevtrout
I store my javascript in a subdirectory of my view folder so the file path is relative to the view being called and I omit the base_url().
我将我的 javascript 存储在我的视图文件夹的子目录中,因此文件路径是相对于被调用的视图,我省略了 base_url()。
Another technique I adopted was to define an array of scripts to include in my controller, then loop through the array in my view to include them. This allows me to include specialty js functions only when needed.
我采用的另一种技术是定义一个脚本数组以包含在我的控制器中,然后在我的视图中循环遍历该数组以包含它们。这允许我仅在需要时包含特殊的 js 函数。
$data['scripts to load'] = array('edit.js','menu.js', 'contact.js');
$this->load->view('myview');
Then in the view
然后在视图中
<?php foreach($scripts_to_load as $script):?>
<script type='text/javascript' src = 'my_js/<?php echo $script;?>'>
<?php endforeach;?>
If you have script files that get loaded on every page, you can hard code them in your footer view like is described in the other answers.
如果您有在每个页面上加载的脚本文件,您可以在页脚视图中对它们进行硬编码,如其他答案中所述。
回答by Bruno
The $data variable sometimes may be lost if you have nested views and you don't pass it as an argument to the children/nested views.
如果您有嵌套视图并且没有将它作为参数传递给子视图/嵌套视图, $data 变量有时可能会丢失。
I found a simple solution that is working very smoothly to me:
我找到了一个对我来说非常顺利的简单解决方案:
In your current view file you setup your script like this:
在您当前的视图文件中,您可以像这样设置脚本:
$this->scripts[] = '/js/myscript.js';
$this->scripts[] = '/js/myscript.js';
at your footer or {whatever.php} file you insert this code:
在您的页脚或 {whatever.php} 文件中插入以下代码:
<?php
if(isset($this->scripts))
foreach($this->scripts as $script) :
?>
<script src="my_asset_path/js/<?=$script;?>"></script>
<?endforeach;?>
If you need only a pice of javascript code, you can always use anonymous functions like this:
如果您只需要一段 javascript 代码,您可以随时使用这样的匿名函数:
<?php
$this->RenderScript[] = function() {
?>
<script>
console.log('myjavascript code snippet');
</script>
<?}?>
and at the bottom:
在底部:
<?php
if(isset($this->RenderScript))
foreach($this->RenderScript as $script) {
$script();
}
?>
回答by Anthony Hyman
Check out Phil Sturgeon's CodeIgniter Template Library. We use a modified version of it at work.
查看 Phil Sturgeon 的CodeIgniter 模板库。我们在工作中使用它的修改版本。
回答by chigley
Just use the standard:
只需使用标准:
<script src="/path/to/file.js" type="text/javascript" charset="utf-8"></script>
inside your view! (Not inside the PHP tags, of course.) I don't think the CodeIgniter HTML helper has any functions that you could use as an alternative to writing out the HTML yourself.
在你的视野中!(当然,不在 PHP 标签内。)我认为 CodeIgniter HTML 帮助程序没有任何功能可以用作替代自己编写 HTML 的功能。