Wednesday, November 4, 2009

JavaScript and Compression




[ Team LiB ]









JavaScript and Compression


JavaScript files are highly compressible, in some cases by as much as 60 to 80 percent. Modern browsers can decompress JavaScripts either in external files or embedded within (X)HTML files. As Chapter 11, "Case Study: DHTML.com," shows, the difference in size and speed can be dramatic. You can compress JavaScript files in two different ways: proprietary and standards-based.


Each browser has its own proprietary way of compressing JavaScripts, related to signed scripts, Java archives, or help file systems. In theory, you could create a sophisticated sniffer to load the appropriate file for the visiting browser, but you'd have to maintain four separate files. A cleaner way is to use standards-based gzip content encoding.


Like HTML, external JavaScripts can be delivered compressed from the server and automatically decompressed by HTTP 1.1-compliant browsers. The only gotchas to watch out for are that external compressed JavaScript files must be referenced within the head element to be reliably decompressed by modern browsers, and Explorer 5 has a subtle onload bug with compressed scripts. You can work around both gotchas, however. You'll learn all the details in Chapter 18, "Compressing the Web."


By grouping external JavaScripts and using compression, you can dramatically reduce their impact on page display speed and bandwidth usage.








    [ Team LiB ]



    No comments:

    Post a Comment