04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 20: Best Practices<br />

This line tells mod_gzip to compress any file ending with .js that is requested from the browser.<br />

Assuming that all of your JavaScript files end with .js , this will compress every request and apply the<br />

appropriate headers to indicate that the contents have been compressed. For more information about<br />

mod_gzip , visit the project site at http://www.sourceforge.net/projects/mod - gzip/ .<br />

For mod_deflate , you can similarly include a single line to ensure that the JavaScript files are<br />

compressed before being sent. Place the following line in either your httpd.conf file or a<br />

.htaccess file:<br />

#Tell mod_deflate to include all JavaScript files<br />

AddOutputFilterByType DEFLATE application/x-<strong>javascript</strong><br />

Note that this line uses the MIME type of the response to determine whether or not to compress it.<br />

Remember that even though text/<strong>javascript</strong> is used for the type attribute of < script > , JavaScript<br />

files are typically served with a MIME type of application/x - <strong>javascript</strong> . For more information on<br />

mod_deflate , visit http://httpd.apache.org/docs/2.0/mod/mod_deflate.html .<br />

Both mod_gzip and mod_deflate result in savings of around 70% of the original file size of JavaScript<br />

files. This is largely due to the fact that JavaScript files are plain text and can therefore be compressed<br />

very efficiently. Decreasing the wire weight of your files decreases the amount of time it takes to transmit<br />

to the browser. Keep in mind that there is a slight trade - off, because the server must spend time<br />

compressing the files on each request, and the browser must take some time to decompress the files once<br />

they arrive. Generally speaking, however, the trade - off is well worth it.<br />

Most web servers, both open source and commercial, have some HTTP compression<br />

capabilities. Please consult the documentation for your server to determine how to<br />

configure compression properly.<br />

Summary<br />

As JavaScript development has matured, best practices have emerged. What once was considered a<br />

hobby is now a legitimate profession and, as such, has experienced the type of research into<br />

maintainability, performance, and deployment traditionally done for other programming languages.<br />

Maintainability in JavaScript has to do partially with the following code conventions:<br />

❑<br />

❑<br />

❑<br />

Code conventions from other languages may be used to determine when to comment and how<br />

to indent, but JavaScript requires some special conventions to make up for the loosely typed<br />

nature of the language.<br />

Since JavaScript must coexist with HTML and CSS, it ’ s also important to let each wholly define<br />

its purpose: JavaScript should define behavior, HTML should define content, and CSS should<br />

define appearance.<br />

Any mixing of these responsibilities can lead to difficult - to - debug errors and maintenance<br />

issues.<br />

666

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!