如何在CodeIgniter中动态加载CS,JS? [英] How to load CS, JS dynamically in CodeIgniter?

查看:71
本文介绍了如何在CodeIgniter中动态加载CS,JS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与不同视图相关的不同CSS和JS文件。如何根据视图有条件地或动态地加载CSS和JS,以便仅将相应的JS或CSS加载到浏览器?

I have different CSS and JS files related to different views. How to load CSS and JS conditionally or dynamically according to views so that only the respective JS or CSS loads to the browser?

推荐答案

这很简单。只需在函数上创建条件即可。然后发送您的CSS或JS文件位置进行查看。

It's simple. Just create condition on your function. Then send your CSS or JS file location to view.

控制器:

public function index() {
    // conditional 
    if (your condition) {
        $this->data = array(
            'css' => site_url() . "your css file",
            'js'  => site_url() . "your js file"
        );
    } else {
        $this->data = array(
            'css' => site_url() . "your other css file",
            'js'  => site_url() . "your other js file"
        );
    }
    $this->load->view('page', $this->data);
}

视图:

<link rel="stylesheet" type="text/css" href="<?php echo $css; ?>">
<script src="<?php echo $js; ?>"></script>

这篇关于如何在CodeIgniter中动态加载CS,JS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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