简体   繁体   English

多个javascript / css文件:最佳做法?

[英]Multiple javascript/css files: best practices?

I have about 7 Javascript files now (thanks to various jQuery plugins) and 4-5 CSS files. 我现在有大约7个Javascript文件(感谢各种jQuery插件)和4-5个CSS文件。 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. YSlow告诉我,Javascript文件应该 - 如果可能 - 最后包括在内。 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. 我所有的Javascript文件都是函数和jQuery代码(所有这些都在ready()时完成)所以应该没问题。

So should I include one CSS and one Javascript file and have those include the rest? 那么我应该包含一个CSS和一个Javascript文件,并包含其余的? Should I concatenate all my files into one? 我应该将所有文件连接成一个吗? Should I put Javascript my tags at the very end of my document? 我应该在文档的最末端放置Javascript我的标签吗?

Edit: FWIW yes this is PHP. 编辑: FWIW是的,这是PHP。

I would suggest using PHP Minify , which lets you create a single HTTP request for a group of JS or CSS files. 我建议使用PHP Minify ,它允许您为一组JS或CSS文件创建单个HTTP请求。 Minify also handles GZipping, Compression, and HTTP Headers for client side caching. Minify还处理客户端缓存的GZipping,压缩和HTTP标头。

Edit: Minify will also allow you to setup the request so that for different pages you can include different files. 编辑:Minify还允许您设置请求,以便对于不同的页面,您可以包含不同的文件。 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. 例如,一组核心JS文件以及某些页面上的自定义JS代码或其他页面上的核心JS文件。

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. 虽然在开发过程中包含了您通常所需的所有文件,然后当您接近切换到生产时,运行minify并将所有CSS和JS文件加入到单个HTTP请求中。 It's really easy to setup and get working with. 它很容易设置和使用。

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. 同样是的,CSS文件应该在头部设置,JS文件在底部提供,因为JS文件可以写入您的页面并且可能导致大量的超时问题。

Here's how you should include your JS files: 这是你应该如何包含你的JS文件:

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

Edit: You can also use Cuzillion to see how your page should be set up. 编辑:您还可以使用Cuzillion来查看您的页面应如何设置。

Here's what I do: I use up to two JavaScript files and generally one CSS file for each page. 这是我的工作:我最多使用两个JavaScript文件,每个页面通常有一个CSS文件。 I figure out which JS files will be common across all of my pages (or enough of them so it's close - the file containing jQuery would be a good candidate) and then I concatenate them and minify them using jsmin-php and then I cache the combined file. 我弄清楚哪些JS文件在我的所有页面中都是通用的(或者它们足够接近 - 包含jQuery的文件将是一个很好的候选者)然后我连接它们并使用jsmin-php缩小它们然后我缓存合并文件。 If there are any JS files left over that are specific to that one page only, I concatenate, minify, and cache them into a single file as well. 如果剩下的任何JS文件仅限于该页面,我会连接,缩小并将它们缓存到单个文件中。 The first JS file will be called over a number of pages, the second only on that one or maybe a few. 第一个JS文件将在多个页面上调用,第二个仅在该页面上调用,或者可能是少数几个。

You can use the same concept with CSS if you like with css-min , though I find I usually only use one file for CSS. 如果你喜欢css-min ,你可以使用与CSS相同的概念,虽然我发现我通常只使用一个文件用于CSS。 One thing extra, when I create the cache file, I put in a little PHP code in the beginning of the file to serve it as a GZipped file, which is actually where you'll get most of your savings anyways. 另外一件事,当我创建缓存文件时,我在文件的开头添加了一些PHP代码,将其作为GZipped文件提供,这实际上是您可以获得大部分积蓄的地方。 You'll also want to set your expiration header so that the user's browser will have a better chance of caching the file as well. 您还需要设置过期标头,以便用户的浏览器也有更好的缓存文件的机会。 I believe you can also enable GZipping through Apache. 我相信你也可以通过Apache启用GZipping。

For the caching, I check to see if the file creation time is older than the amount of time that I set. 对于缓存,我检查文件创建时间是否早于我设置的时间。 If it is, I recreate the cache file and serve it, otherwise I just get the existing cached file. 如果是,我重新创建缓存文件并提供它,否则我只是获取现有的缓存文件。

You haven't explicitly said that you've got access to a server-side solution, but assuming you do, I've always gone with a method involving using PHP to do the following: 您没有明确表示您可以访问服务器端解决方案,但假设您这样做,我总是使用涉及使用PHP执行以下操作的方法:

jquery.js.php: jquery.js.php:

<?php
$jquery = ($_GET['r']) ? explode(',', $_GET['r']) : array('core', 'effects', 'browser', 'cookies', 'center', 'shuffle', 'filestyle', 'metadata');
foreach($jquery as $file)
 {
echo file_get_contents('jquery.' . $file . '.js');
 }
?>

With the snippet above in place, I then call the file just like I normally would: 使用上面的代码片段,然后我就像通常那样调用文件:

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

and then if I'm ever aware of the precise functionality I'm going to need, I just pass in my requirements as a query string ( jquery.js.php?r=core,effects ). 然后,如果我意识到我将需要的精确功能,我只需将我的要求作为查询字符串传递( jquery.js.php?r = core,effects )。 I do the exact same for my CSS requirements if they're ever as branched. 如果我们的CSS要求是如此,那么我的CSS要求完全相同。

我不建议使用基于javascript的解决方案(如PHP Minify)来包含您的CSS,因为如果访问者禁用了javascript,您的页面将无法使用。

The idea of minifying and combining the files is great. 缩小和组合文件的想法很棒。

I do something similar on my sites but to ease development I suggest some code which looks like this: 我在我的网站上做了类似的事情,但为了简化开发我建议使用如下代码:

if (evironment == production) {
  echo "<style>@import(/Styles/Combined.css);</style>"
} else {
  echo "<style>@import(/Styles/File1.css);</style>"
  echo "<style>@import(/Styles/File2.css);</style>"
}

This should let you keep your files separate during dev for easy management and use the combined file during deployment for quicker page loads. 这应该允许您在开发期间将文件分开以便于管理,并在部署期间使用组合文件以加快页面加载速度。 This assumes you have the ability to combine the files and change variables as part of your deploy process. 这假设您可以将文件和更改变量组合在一起作为部署过程的一部分。

Definitely look into including your js at the bottom and the css at the top as per YUI recommendations as keeping the JS low has a tangible affect on the appearance of the rest of the page and feels much faster. 根据YUI建议,绝对考虑将js放在底部,将css放在顶部,因为保持JS低对页面其余部分的外观有明显影响,感觉更快。

I also tend to copy+paste all of my jquery plugins into a single file: jquery.plugins.js then link to 我也倾向于将我的所有jquery插件复制+粘贴到一个文件中:jquery.plugins.js然后链接到

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js">

for the actual jquery library. 对于实际的jquery库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM