dou91808 2010-07-10 09:33
浏览 39
已采纳

$ not defined - javascript错误,试图优化文件

HI guys, my application uses a lot of different javascript files. SO I tried to be quick about it and accomodate all the files in one single file. This I did by creating a single php with the following code:

<?php
header('Content-type: text/javascript');


$js = array( ....   ); // all files to include

$js = array_unique($js);

foreach($js as $oneJs)
    require_once('scripts/'.$oneJs);

And including a reference to this file where I need to include the javascript as:

<script type="text/javascript" src="includes/js.php"></script>

I'm using prototype js here and the scriptaculous libraries - the problem is that I'm getting loads of errors and now half my javascritp wont run. Especially now I'm getting the $ is not defined error. I've included prototype in the list but I'm still getting this error - help please!!!

  • 写回答

2条回答 默认 最新

  • dongzhou8764 2010-07-10 09:38
    关注

    You need to be careful of two things when doing this.

    1. Make sure the loading order is correct. If you have script files that use jQuery, jQuery needs to be loaded first. The $ is not defined error sounds like jQuery is loaded too late.

      What I sometimes do for this is add numbers to the names (1_jquery.js, 2_dialog.js....) and then do a sort() on the array.

      You could also consider using an array to specify the order of the most important javascript includes, and load the rest in arbitrary order.

    2. Make sure there are no "misunderstood" statements because of missing ";"s at the end of each script file that become glued together. It's a good practice to echo a standalone ";" in between each included file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题