What is the best method to reduce the size of my Javascript and CSS files?

后端 未结 18 967
礼貌的吻别
礼貌的吻别 2020-11-30 04:16

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

相关标签:
18条回答
  • 2020-11-30 04:35

    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;
    

    ...instead of:

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

    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;}
    

    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.

    Including Javascript

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

    Including CSS

    <link rel="stylesheet" href="/css/myStyle.css" type="text/css" media="All" />
    
    0 讨论(0)
  • 2020-11-30 04:36

    YUI Compressor has my vote, for the simple reason that instead of just performing regular expression transformations on the code, it actually builds a parse tree of the code, similar to a Javascript interpreter, and then compresses it that way. It is usually very careful about how it handles the compression of identifiers.

    Additionally it has a CSS compression mode, which I haven't played with as much.

    0 讨论(0)
  • 2020-11-30 04:38

    I found JSCompress a nice way to not only minify a JavaScript, but to combine multiple scripts. Useful if you're only using the various scripts once. Saved 70% before compression (and something similar after compression too).

    Remember to add back in any copyright notices afterwards.

    0 讨论(0)
  • 2020-11-30 04:40

    Compression and minify-ing (removing whitespace) are a start.

    Additionally:

    1. Combine all of your JavaScript and CSS includes into a single file. That way the browser can download the source in a single request to server. Make this part of your build process.

    2. Turn caching on at the web-server level using the cache-control http header. Set the expiry to a large value (like a year) so the browser will only download the source once. To allow for future edits, include a version number on the query-string, like this:

    <script src="my_js_file.js?1.2.0.1" type="text/javascript"></script>

    <link rel="stylesheet" type="text/css" href="my_css_file.css?3.1.0.926" />

    0 讨论(0)
  • 2020-11-30 04:42

    I'd give a test-drive to the new runtime optimizers in ASP.Net published on http://www.codeplex.com/NCOptimizer

    0 讨论(0)
  • 2020-11-30 04:43

    Google hosts a handful of pre-compressed JavaScript library files that you can include in your own site. Not only does Google provide the bandwidth for this, but based on most browser's file caching algorithms, if the user has already downloaded the file from Google for another site they won't have to do it again for yours. A nice little bonus for some of the 90k+ JS libraries out there.

    0 讨论(0)
提交回复
热议问题