将CSS和JavaScript放在文件或主HTML中? [英] Put CSS and JavaScript in files or main HTML?

查看:156
本文介绍了将CSS和JavaScript放在文件或主HTML中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然始终建议将JavaScript和CSS代码放入适当的文件(如 .js .css ),大多数主要网站(如亚马逊,Facebook等)将他们的JavaScript和CSS代码的很大一部分直接放在主HTML页面中。

Although it is always recommended to put JavaScript and CSS code into appropriate files (as .js and .css), most of major websites (like Amazon, facebook, etc.) put a significant part of their JavaScript and CSS code directly within the main HTML page.

最佳选择?

推荐答案

http://developer.yahoo.com/performance/rules.html#external

Yahoo(即使它们有很多内联样式和脚本) ,建议将其置于外部。我相信谷歌页面的速度过去(或仍然?)做同样好。

Yahoo (even though they have many inline styles and scripts), recommends making them external. I believe google page speed used to (or still does?) do the same as well.

这是一个合乎逻辑的事情,让他们分开。将CSS和JS分离到HTML有很多好处。像逻辑代码管理,缓存这些页面,页面大小(你宁愿一个〜200ms请求一个400kb的缓存资源,或4000ms的延迟,不得不下载数据在每一个页面?),SEO选项(更少的垃圾google来看看脚本/样式是外部的),更容易缩小外部脚本(在线工具等),可以从不同的服务器同步加载....

It's really a logical thing to have them separate. There are so many benefits to keeping CSS and JS separate to the HTML. Things like logical code management, caching of those pages, lower page size (would you rather a ~200ms request for a 400kb cached resource, or a 4000ms delay from having to download that data on every page?), SEO options (less crap for google to look through when scripts/styles are external), easier to minify external scripts (online tools etc), can load them synchronously from different servers....

应该是您在任何网站的主要目标。组成整个网站的所有样式都应在一个文件中(或每个页面的文件,然后在更新时合并和缩小),同样适用于javascript。

That should be your primary objective in any website. All styles that make up your whole website should be in the one file (or files for each page, then merged and minified when updated), the same for javascript.

现实世界(不是为自己做一个项目,为需要结果的客户或利益相关者做一个项目),唯一的一个时候,加载另一个javascript资源或另一个样式表(因此使用内联样式/ javascript )是如果存在某种类型的每用户,每会话或每时间段的动态信息,其不能以任何其他方式完成。示例:当我的网站有促销活动时,我们转储一个具有小JSON信息对象的脚本标签。因为我们不会缩小和合并多个文件,所以将其包含在页面中更有意义。当然有其他方法可以做到这一点,但它花费20美元,但它可能花费> $ 100以另一种方式。

In the real world (not doing a project for yourself, doing one for a client or stakeholder that wants results), the only time where it doesn't make sense to load in another javascript resource or another stylesheet (and thus use inline styles/javascript) is if there's some kind of dynamic information that is on a kind of per-user, per-session or per-time-period that can't be accomplished as simply any other way. Example: when my website has a promotion, we dump a script tag with a small JSON object of information. Because we don't minify and merge multiple files, it makes more sense to just include it in the page. Sure there are other ways to do this, but it costs $20 to do that, whereas it could cost > $100 to do it another way.

也许亚马逊/ Facebook /谷歌等使用这么多的内联代码是他们的服务器不纳税这么多。我不太确定在一个命中请求1MB文件或请求10个100KB文件(假设1MB / 10 = 100KB的例子)之间的基准,但什么会更快?潜在的1MB文件,但是较小的请求可以同步加载,这意味着这10个请求中的每一个都可能来自单独的服务器/域,从而减少总体加载时间。

Perhaps Amazon/Facebook/Google etc use so much inline code is so their servers aren't taxed so much. I'm not too sure on the benchmarking between requesting a 1MB file in one hit or requesting 10 100KB files (presuming 1MB/10 = 100KB for examples' sake), but what would be faster? Potentially the 1MB file, BUT smaller requests can be loaded synchronously, meaning each one of those 10 requests could come from a separate server/domain potentially, thus reducing overall load time.

另外,例如google主页似乎转储了一个JSON数组的信息为小部件,可能是因为它编译所有来自各种来源的信息,缩小它,缓存它,然后放在页面上,然后javascript函数构建布局(客户端处理能力而不是服务器端)。

Further, google homepages for example seem to dump a JSON array of information for the widgets, presumably because it compiles all that information from various sources, minifies it, caches it, then puts in on the page, then the javascript functions build the layout (client side processing power rather than server-side).

这篇关于将CSS和JavaScript放在文件或主HTML中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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