多个javascript / css文件:最佳做法? [英] Multiple javascript/css files: best practices?

查看:112
本文介绍了多个javascript / css文件:最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有大约7个Javascript文件(感谢各种jQuery插件)和4-5个CSS文件。我很好奇什么是处理这些的最佳实践,包括在文档中他们应该加载什么? YSlow告诉我,Javascript文件应该尽可能地包含在结尾。身体的尽头?它提到,测距仪似乎是他们写内容。所有我的Javascript文件是函数和jQuery代码(所有完成当ready()),所以应该是确定。

I have about 7 Javascript files now (thanks to various jQuery plugins) and 4-5 CSS files. I'm curious as to what's the best practice for dealing with these including where in the document they should be loaded? YSlow tells me that Javascript files should be--where possible--included at the end. The end of the body? It mentions that the delimeter seems to be whether they write content. All my Javascript files are functions and jQuery code (all done when ready()) so that should be OK.

所以我应该包括一个CSS和一个Javascript文件,那些包括其余?我应该把所有的文件连接成一个吗?

So should I include one CSS and one Javascript file and have those include the rest? Should I concatenate all my files into one? Should I put Javascript my tags at the very end of my document?

编辑:FWIW是的,这是PHP。

FWIW yes this is PHP.

推荐答案

我建议使用 PHP Minify ,它允许您为一组JS或CSS文件创建单个HTTP请求。 Minify还处理用于客户端缓存的GZipping,Compression和HTTP标头。

I would suggest using PHP Minify, which lets you create a single HTTP request for a group of JS or CSS files. Minify also handles GZipping, Compression, and HTTP Headers for client side caching.

编辑:Minify还允许您设置请求,以便为不同的网页添加不同的文件。例如,一组核心JS文件以及某些页面上的自定义JS代码或其他页面上的核心JS文件。

Minify will also allow you to setup the request so that for different pages you can include different files. For example a core set of JS files along with custom JS code on certain pages or just the core JS files on other pages.

开发时包括所有文件通常会,然后当你更接近切换到生产运行minify和连接所有的CSS和JS文件到一个单一的HTTP请求。

While in development include all the files as you normally would and then when you get closer to switching to production run minify and join all the CSS and JS files into a single HTTP request. It's really easy to setup and get working with.

还有,CSS文件应该设置在头部,JS文件放在底部,因为JS文件可以写入

Also yes, CSS files should be set in the head, and JS files served at the bottom, since JS files can write to your page and can cause massive time-out issues.

您应该如何添加您的JS文件:

Here's how you should include your JS files:

</div> <!-- Closing Footer Div -->
<script type="application/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js"></script>
</body>
</html>

编辑:您也可以使用 Cuzillion 查看您的信息页应如何设置。

You can also use Cuzillion to see how your page should be set up.

这篇关于多个javascript / css文件:最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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