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 />

Modern web applications use JavaScript to change styles frequently, so although it ’ s not possible to<br />

completely decouple CSS and JavaScript, the coupling can be made looser. This is done by dynamically<br />

changing classes instead of individual styles, as in the following example:<br />

//loose coupling of CSS to JavaScript<br />

element.className = “edit”;<br />

By changing only the CSS class of an element, you allow most of the style information to remain strictly<br />

in the CSS. JavaScript can be used to change the class, but it ’ s not directly affecting the style of the<br />

element. As long as the correct class is applied, then any display issues can be tracked directly to CSS<br />

and not to JavaScript.<br />

The second type of tight coupling is valid only in Internet Explorer (but not in IE 8 running in standards<br />

mode), where it ’ s possible to embed JavaScript in CSS via expressions, as in this example:<br />

/* tight coupling of JavaScript to CSS */<br />

div {<br />

width: expression(document.body.offsetWidth - 10 + “px”);<br />

}<br />

Expressions are typically avoided because they ’ re not cross - browser – compatible. They should also be<br />

avoided because of the tight coupling between JavaScript and CSS that they introduce. Using<br />

expressions, it ’ s possible that a JavaScript error can occur in CSS. Developers who have tried to track<br />

down a JavaScript error due to CSS expressions can tell you how long it took before they even<br />

considered looking at the CSS for the source of the error.<br />

Once again, the importance of keeping a good separation of layers is paramount. The only source for<br />

display issues should be CSS, and the only source for behavior issues should be JavaScript. Keeping a<br />

loose coupling between these layers makes your entire application more maintainable.<br />

Decouple Application Logic/Event Handlers<br />

Every web application is typically filled with lots of event handlers listening for numerous different<br />

events. Few of them, however, take care to separate application logic from event handlers. Consider the<br />

following example:<br />

function handleKeyPress(event){<br />

if (event.keyCode == 13){<br />

var target = EventUtil.getTarget(event);<br />

var value = 5 * parseInt(target.value);<br />

if (value > 10){<br />

document.getElementById(“error-msg”).style.display = “block”;<br />

}<br />

}<br />

}<br />

This event handler contains application logic in addition to handling the event. The problem with this<br />

approach is twofold. First, there is no way to cause the application logic to occur other than through the<br />

event, which makes it difficult to debug. What if the anticipated result didn ’ t occur? Does that mean<br />

that the event handler wasn ’ t called or that the application logic failed? Second, if a subsequent event<br />

causes the same application logic to occur, you ’ ll need to duplicate the functionality or else extract it into<br />

a separate function. Either way, it requires more changes to be made than are really necessary.<br />

641

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

Saved successfully!

Ooh no, something went wrong!