什么是最好的方法来减少我的JavaScript和CSS文件的大小? [英] What is the best method to reduce the size of my Javascript and CSS files?

查看:223
本文介绍了什么是最好的方法来减少我的JavaScript和CSS文件的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用大型和/或许多Javascript和CSS文件时,减小文件大小的最佳方法是什么?

When working with large and/or many Javascript and CSS files, what's the best way to reduce the file sizes?

推荐答案

除了使用服务器端压缩,使用智能编码是保持带宽成本低的最好方法。您可以随时使用 Dean Edward的Javascript封装等工具,但对于CSS,请花时间了解 CSS速。例如。使用:

In addition to using server side compression, using intelligent coding is the best way to keep bandwidth costs low. You can always use tools like Dean Edward's Javascript Packer, but for CSS, take the time to learn CSS Shorthand. E.g. use:

background: #fff url(image.gif) no-repeat top left;

...而不是:

background-color: #fff;
background-image: url(image.gif);
background-repeat: no-repeat;
background-position: top left;

此外,使用CSS的级联性质。例如,如果您知道您的网站将使用一个font-family,请为body标签中的所有元素定义如下:

Also, use the cascading nature of CSS. For example, if you know that your site will use one font-family, define that for all elements that are in the body tag like this:

body{font-family:arial;}

其他可以帮助的事情是将CSS和JavaScript作为文件,而不是内联或在每个页面的头部。

One other thing that can help is including your CSS and JavaScript as files instead of inline or at the head of each page. That way your server only has to serve them once to the browser after that browser will go from cache.

<script type="text/javascript" src="/scripts/loginChecker.js"></script>



包括CSS



Including CSS

<link rel="stylesheet" href="/css/myStyle.css" type="text/css" media="All" />

这篇关于什么是最好的方法来减少我的JavaScript和CSS文件的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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