我知道这个问题已经问了好几次,但我似乎找不到适当的解决办法。
我使用的是CodeIgniter,一个遵循MVC ()原则的PHP框架。
我有许多javascript文件,如jQuery和其他泛型库,对于head标记中包含的每个页面,我都需要这些文件。我还有特定于视图的javascript,这些javascript直接嵌入到脚本块中的视图中。
视图customerInfo.php的一个示例
<script type="text/javascript">
    // some javascript here for customerInfo view
    // I MUST be able to use PHP variables in this script block ex:
    var customerName = "<?php echo $customerName; ?>";
</script>
<p class="someClass">Hello <?=$customerName?>, may the lord provide you with bananas</p>我的网站布局如下:
<html>
<head>
    <script src="http://coolStorybro.com/jquery.js" type="text/javascript"></script>
    <script src="http://coolStorybro.com/commonStuff.js" type="text/javascript"></script>
</head>
<body>
<div class="header">some header stuff</div>
<div class="pageContent">
   <?php echo $view; ?>   // In this case, $view will hold the view customerInfo
</div>
</body>
</html>我的目标是将我所有的javascript放在同一个地方,这样我就可以缩小它,并最终将它放在页面底部,以便加载优化。
我知道,我可以将视图特定的javascript存储在一个名为customerInfo.js的单独文件中,并将其加载到head标记中,但我觉得为每个视图创建单独的javascript文件太麻烦了,我将无法在其中使用PHP,这是我必须做到的。
所以我要问你们,我的问题有解决办法吗?还是我注定要把我的页面聚集在脚本块的周围?
谢谢您抽时间见我。
编辑:
是否有一种方法可以将视图特定的javascript (所有视图)放在一个文件中,并以某种方式将其分离为模块,并且只为所需的视图加载特定的模块?
假设viewsJS.js保存所有特定于视图的javascript:
module ("customerInfo", 
    $(".someClass").html("Bla bla bla");
);
module ("invoiceInfo", 
    // Some code to be executed when the invoiceInfo module is loaded
);一旦为每个模块(视图)定义了javascript,就可以将它们加载到相应的视图中。问题是,在我的视图中不能有一个脚本块来“加载”模块,所以..。我太笨了。
发布于 2013-03-18 14:57:31
在application/views中创建此文件夹结构
/includes/javascript.php
/template.php然后将主代码放在template.php中。
<html>
<head>
  <?php echo $this->load->view('includes/javascript'); ?>
</head>
<body>
<div class="header">some header stuff</div>
...并将所有javascript放在javascript.php中
application/views/includes例如:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>在浏览器中,finale输出如下所示:
<html>
<head>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
</head>
<body>
<div class="header">some header stuff</div>
...为了加载特定的javascript文件,您可以在javascript视图中执行类似的操作:
<?php if (is_frontpage()) : ?>
   <script src="http://localhost/javascriptforfrontpage.js"></script>
<?php endif; ?>
<?php if (is_contact()) : ?>
   <script src="http://localhost/javscriptforcontactpage.js"></script>
<?php endif; ?>
<?php // this would be loaded everywhere ?>
<script src="http://localhost/javascripteverypage.js"></script>只加载客户信息javascript:
首先,您从控制器发送到您的视图$data['load_js_customer_info'],例如:
public function customerInfo()
    {
        $data['load_js_customer_info'] = "Customer Info";
        $data['customer'] = $this->mdl_admin->get_customer();
        $this->load->view('template', $data);
    }在视图(包含所有javascript的内容)中,您创建了一个条件:
 <?php if ($load_js_customer_info) : ?>
   <script src="http://localhost/javscriptforcustomerinfo.js"></script>
 <?php endif; ?>因此,如果不是来自customer控制器,则不会加载javscript。
https://stackoverflow.com/questions/15479631
复制相似问题