16 Interactivity


16.1 Links: the <a> element

SVG provides an <a> element, analogous to like HTML's <a> element, to indicate hyperlinks; those parts of the drawing which when clicked on will cause the current browser frame to be replaced by the contents of the URL specified in the href attribute.

The <a> element uses Xlink. (Note that the XLink specification is currently under development and is subject to change. The SVG working group will track and rationalize with XLink as it evolves.)

The following is a valid example of a hyperlink attached to a path (which in this case draws a triangle):

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG June 1999//EN" 
  "http://www.w3.org/Graphics/SVG/svg-19990625.dtd">
<svg width="4in" height="3in">
  <desc>This valid svg document draws a triangle which is a hyperlink
  </desc>
  <a href="http://www.w3.org">
    <p d="M 0 0 L 200 0 L 100 200 Z"/>
  </a>
</svg>

Download this example

This is the well-formed equivalent example:

<?xml version="1.0" standalone="yes"?>
<svg width="4in" height="3in"
 xmlns = 'http://www.w3.org/Graphics/SVG/svg-19990625.dtd'>
  <desc>This well formed svg document draws a triangle which is a hyperlink
  </desc>
  <a xml:link="simple" show="replace" actuate="user" href="http://www.w3.org">
    <p d="M 0 0 L 200 0 L 100 200 Z"/>
  </a>
</svg>

Download this example

In both examples, if the path is clicked on, then the current browser frame will be replaced by the W3C home page.

16.2 Event Handling

Any <g>, <image>, <path>, <text> or vector graphic shape (such as a <rect>) can be assigned any of the following standard HTML event handlers:

Mouse Events

Keyboard Events

State Change Events

Additionally, SVG's scripting engine needs to have the altKey, ctrlKey and shiftKey properties available.

16.3 Zoom and pan control

The outermost <svg> element in an SVG document can have the optional attribute allow-zoom-and-pan, which takes the possible values of trueand false. If true, the user agent should allow the user to zoom in and pan around the given document. If false, the user agent should not allow the user to zoom and pan on the given document.