Web server logs - 153Chapter 13JavaScript Essentialsoptimized for usage through relatively slow
153Chapter 13JavaScript Essentialsoptimized for usage through relatively slow network connections and viewing on smallscreens. Numerous other browsers are designed to provide Web accessibility for userswith disabilities through technologies such as speech synthesis and touch screens (seehttp://www.w3.org/WAI). If such browsers do not understand the tag forrefreshing content, they land at this page with no further automatic processing. But bycreating an image that acts as a link, the user will likely click (or tap) on it to continue. The link then leads to the nonscriptable home page. Also note that the altattribute forthe image is supplied. This takes care of Lynx and compact browsers (with image load- ing off) because these browsers show the altattribute text in lieu of the image. Usersclick or tap on the text to navigate to the URL referenced in the link tag. I have a good reason to keep the background of the branching index page plain. For thosewhose browsers automatically lead them to a content-filled home page, the browser windowflashes from a set background color to the browser s default background color before the newhome page and its background color appear. By keeping the initial content to only the com- pany logo, less screen flashing and obvious navigation are visible to the user. One link alternate destinationsAnother filtering technique is available directly from links. With the exceptions of NN2 andIE3, a link can navigate to one destination via a link s onclickevent handler and to anothervia the hrefattribute if the browser is not scriptable. The trick is to include an extra returnfalsestatement in the onclickevent handler. Thisstatement cancels the link action of the hrefattribute. For example, if a nonscriptablebrowser should go to one version of a page at the click of a link and the scriptable browsershould go to another, the link tag is as follows: Product Catalog Only nonscriptable browsers, NN2, and IE3 go to the nonJSCatalog.htmlpage; all others goto the JSCatalog.htmlpage. Object detectionThe final methodology for implementing browser version branching is known as object detec- tion. The principle is simple: If an object type exists in the browser s object model, it is safe toexecute script statements that work with that object. Perhaps the best example of object detection is the way scripts can swap images on a page innewer browsers without tripping up on older browsers that don t implement images as objects. In a typical image swap, onmouseoverand onmouseoutevent handlers (assigned to a link sur- rounding an image, to be backward compatible) invoke functions that change the srcpropertyof the desired image. Each of those functions is invoked for all scriptable browsers, but youwant them to run their statements only when images can be treated as objects. Object models that implement images always include an array of image objects belonging tothe documentobject. The document.imagesarray always exists, even with a length of zerowhen no images are on the page. Therefore, if you wrap the image swapping statementsinside an ifconstruction that lets browsers pass only if the document.imagesarray exists, older browsers simply skip over the statements: function imageSwap(imgName, url) { if (document.images) { document.images[imgName].src = url; } }