Chapter 19. Link and Anchor Objects


  • Differences among link, anchor, and a element objects

  • Scripting a link to invoke a script function

  • Scripting a link to swap an image on mouse rollovers

The Web is based on the notion that the world's information can be strung together by way of the hyperlink—the clickable hunk of text or image that enables an inquisitive reader to navigate to a further explanation or related material. Of all the document objects you work with in JavaScript, the link is the one that makes that connection. Anchors also provide guideposts to specific locations within documents.

As scriptable objects going back to the first scriptable browsers, links and anchors are comparatively simple devices. But this simplicity belies their significance in the entire scheme of the Web. Under script control, links can be far more powerful than mere tethers to locations on the Web.

In modern browsers, the notion of separating links and anchors as similar yet distinctly different objects begins to fade. The association of the word link with objects is potentially confused by the newer browsers' recognition of the link element (see Chapter 37 on the CD-ROM), which has an entirely different purpose, as a scriptable object. Taking the place of the anchor and link objects is an HTML element object representing the element created by the <a> tag. As an element object, the a element assumes all of the properties, methods, and event handlers that accrue to all HTML element objects ...

Get JavaScript® Bible, Sixth Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.