AJAX - The Complete Reference

(avery) #1

PART II


Chapter 6: Networking Considerations 267


this value, it is necessary to ensure that all related script and link references are modified as
well. These may even be located in other files, including libraries, so be careful.
Changing class values is not quite as dangerous, since experience shows that most
JavaScript developers tend not to manipulate class values as often as they do id values.
However, class name reduction ultimately suffers from the same problem as id reduction,
so again, be careful.

NNOT EOTE You might be tempted to remap name attributes, particularly on form fields, since these
values are also operated on by server-side programs that would have to be altered as well. This
would of course impact the application architecture more than some other optimizations, so
proceed with caution.

JavaScript Optimizations
Given Ajax’s intrinsic use of JavaScript, often lots of it, you might need to really concentrate
on reducing your script footprint. Many of the techniques for JavaScript optimization are
similar to those used for markup and CSS. However, JavaScript optimization must be
performed far more carefully because, if it is done improperly, the result is not just a visual
distortion, but potentially a broken page! Let’s start with the most obvious and easiest
improvements and then move on to ones that require greater care.


  1. Remove JavaScript comments.
    Except for the masking comment, all JavaScript comments indicated
    by // or / / can safely be removed, as they offer no value to end users (except
    for the ones who want to understand how your script works). The comment
    removal may make the script less of a teacher both to those who want to learn
    legitimately and those who want to figure out an exploit.

  2. Remove whitespace in JavaScript.
    Interestingly, whitespace removal in JavaScript is not nearly as beneficial as it might
    seem. Certainly code like:


x = x + 1;
can obviously be reduced to:

x=x+1;
However, because of the common sloppy coding practice of JavaScript developers
failing to terminate lines with semicolons, whitespace reduction can cause problems.
For example, given the following legal JavaScript below, which uses implied
semicolons:

x=x+1
y=y+1
a simple whitespace remover tool might produce:

x=x+1y=y+1
which would obviously throw an error. By adding in the needed semicolons to
produce:

x=x+1;y=y+1
Free download pdf