在Code Igniter中加载JavaScript [英] Loading javascript in Code Igniter

查看:30
本文介绍了在Code Igniter中加载JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我们的Code Igniter应用程序,我们正要在结束body标记之前加载所有JavaScript.

For our Code Igniter application we are loading all of our javascript just before the closing body tag.

因此在我们的控制器中,我们拥有

so in our controllers we have

$this->load->view('head', $this->head);
$this->load->view('main_view_for_controller', $data);
$this->load->view('foot', $this->foot);

在外观上,我们有一堆< script src ="master.js"></script> 标记.

And in the foot view we have a bunch of <script src="master.js"></script> tags.

这些包括

  • jQuery
  • jQuery-ui
  • 共享功能

现在这很好用,直到您想到仅在特定页面或内联js上使用的JS.
您不能只将javascript转储到页面中的任何地方,因为它通常会使用您在底部加载的部分代码.

Now this works great, until you think about JS used only on specific pages, or inline js.
You can't just dump your javascript anywhere in the page as it will generally use bit's and pieces of the parts you load at the bottom.

所以我现在要做的是,
我有/application/views/js/,其中会有类似 login.php

So what I do at the moment is,
I have /application/views/js/ where I will have something like login.php

login.php 可能包含例如

<script>
$(function(){

    var user_id = <?php echo $this->user->get('id'); ?>;
    var return = '<?php echo $return_url; ?>';

    $('#login form').submit(function(){[...]});

    $('#login .facebook').click(function(){[...]});
});
</script>    

所以在我的控制器中,我会打电话给
$ this-> foot ['js'] [] = javascript('login',array('return_url'=>'/users'));

so in my controller I would call
$this->foot['js'][] = javascript('login', array('return_url' => '/users'));

//source of function javascript() from inside a helper
function javascript($file, $config = array()){
    return $this->load->view('js/'.$file, $config, true);
}

然后在我的脚位视图中找到所有我拥有的所有其他文件(在prod env上将其合并为一个文件,然后缩小)

then in my foot view after all the other files (which on the prod env are merged into one file and then minified) I have

foreach($ js as $ jsOut)echo $ jsOut;

只会吐出所有javascript.

which simply spits out all the javascript.

这是最好的方法吗?或者,还有更好的方法?这似乎有点混乱...

Is this the best way of doing this? or is there a better way? This just seems kind of messy...

推荐答案

一个好主意可能是使用页面段来确定要包括的脚本.您不必定义所有脚本,也不必始终填充数组,而是可以定义脚本以及它们属于哪些页面,然后只需检查页面段即可确定要包括的JS脚本.在此处阅读该文档.

A good idea might be to use page segments to determine what scripts to include. Rather than having to populate an array all of the time, you could define your scripts and what pages they belong too, then just check the page segments to determine what JS scripts to include. Read the documentation for the URI class here.

因此,在您的包含中,您将执行以下操作.

So in your include you'd do something like this.

<?php if ( $this->$this->uri->rsegment(2) == 'login' ): ?>
// Your login page specific scripts here
<?php endif; ?>

<?php if ( $this->$this->uri->rsegment(2) == 'home' ): ?>
// Your homepage specific scripts here
<?php endif; ?>

将数字2替换为与您所在页面相关的任何段.

Replace the number 2's with whatever segment relates to the page you're on.

这篇关于在Code Igniter中加载JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆