为什么将我所有的 jquery 插件放入一个文件中不起作用?
之前,我有这样的情况:
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/json2.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery-msdropdown/js/jquery.dd.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.scrollTo-1.4.2/jquery.scrollTo-min.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery-ui-1.8.7.custom/js/jquery-ui-1.8.7.custom.min.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/alertbar.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.masonry.min.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/fileuploader.js" type="text/javascript"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.jeditable.mini.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.growfield2.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.placeholder.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.color.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/tipsy/src/javascripts/jquery.tipsy.js"></script>
加载页面时一切正常,并且没有错误。我的朋友告诉我将它们全部合并到 1 个文件中。所以我这样做了:
find somedir/ -name '*.js' -exec cat {} + > ../allplugins.js
现在,当我包含“allplugins.js”时,我遇到了未捕获的类型错误。对象#没有方法“可编辑”
Before, I had this:
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/json2.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery-msdropdown/js/jquery.dd.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.scrollTo-1.4.2/jquery.scrollTo-min.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery-ui-1.8.7.custom/js/jquery-ui-1.8.7.custom.min.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/alertbar.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.masonry.min.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/fileuploader.js" type="text/javascript"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.jeditable.mini.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.growfield2.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.placeholder.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.color.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/plugins/tipsy/src/javascripts/jquery.tipsy.js"></script>
Everything works when I load the page, and there are no errors. My friend told me to merge them all into 1 file. So I did:
find somedir/ -name '*.js' -exec cat {} + > ../allplugins.js
Now, when I include "allplugins.js", I'm getting Uncaught type errors. Object # has not method "editable"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该按照导入 JavaScript 插件的顺序连接它们。
您可以通过将它们列在一个文件(例如plugins.list)中来做到这一点,每个文件名一行。然后使用以下方法构建 allplugins.js:
例如,如果您有一个文件 inc.js,
其中包含 script.js 尝试使用 inc.js 中定义的对象:
如果颠倒这两个文件的顺序,您可以看到该 obj 正在使用但尚未定义。
You should concatenate your javascript plugins in the same order that you import them.
You can do that by listing them in a file (say plugins.list), one line for each filename. Then build allplugins.js using:
For example, if you have a file inc.js that has
And then script.js that tries to use the object defined in inc.js:
If you reverse the order of those two files, you can see that obj is being used but has not yet been defined.
由于依赖于其他文件,合并的文件的顺序不正确。以与之前相同的顺序合并。
The files merged are not in a proper order due to dependency on other. merge with the same order as previously it was working.
编辑抱歉 - 关于使用“>>”的内容而不是“>”可能是不必要的,因为重定向发生在“find”命令的级别,所以它应该可以正常工作。对此感到抱歉。
应该是:(
也就是说,“>>”而不仅仅是“>”。)在前面加上:
或达到相同效果的东西,否则每次构建时都会使文件更大。
是,文件将根据可能完全任意的排序进行连接,而它们几乎肯定具有需要特定部分排序的相互依赖性。您应该重命名树中的文件,以便简单的串联命令以正确的顺序执行其操作,或者以正确的顺序显式列出文件以进行串联。您拥有的脚本标签的原始序列将是文件排序的一个很好的起点。
另外,找到你的朋友并向他/她扔一个水气球。
edit sorry — the stuff about using ">>" instead of ">" is probably unnecessary, as the redirection happens at the level of the "find" command so it should work out OK. Sorry about that.
Should be:
(That is, ">>" instead of just ">".) Precede that with:
or something to the same effect, or else each time you build you'll make the file bigger.
Another problem you'll face is that the files will be concatenated based on an ordering that is likely to be completely arbitrary, while they almost certainly have interdependencies that call for a particular partial ordering. You should either rename the files in your tree such that that simple concatenation command does its thing in the right order, or else explicitly list the files out in the right order for concatenation. The original sequence of script tags you've got would be a nice starting point for the ordering of your files.
Also, find your friend and throw a water balloon at him/her.
我注意到你用 django 标记了这个问题。您可能对 django-mediasync 感兴趣,它自动处理 js 文件串联你(参见http://pypi.python.org/pypi/ django-mediasync/2.0.0#joined-files):
这旨在通过 Amazon S3 之类的方式使用静态内容站点,但它也可以用于仅从本地服务器提供静态媒体,同时自动处理文件联系。
I noticed you tagged this question with django. You may be interested in django-mediasync which automatically handles js file concatenation for you (see http://pypi.python.org/pypi/django-mediasync/2.0.0#joined-files):
This is designed to use a static content site via something like Amazon S3, but it could also be used to just serve the static media from your local server, while automagically handling file contactenation.