XML technologies / SVG / Interactive graphics / The event and object model in detail / The SVG event model
(Excerpt from “Das Einsteigerseminar SVG — Webgrafiken mit XML” by Helma Spona)
The event model determines how events are processed. As in JavaScript, there are two approaches. Earlier examples used the first: attributes on tags whose events should be handled. The attributes onclick, onmouseover and onmouseout were already explained. Tables describe other event attributes, grouped by use because not every tag supports all of them.
Table: Event attributes for graphic and text elements.
Attribute | Meaning |
|---|---|
| Occurs when the element gains focus, becoming active. |
| Occurs when the element loses focus. |
| Occurs when the element is activated. |
| Occurs when the element is clicked. |
| Occurs when the mouse button is pressed on the element. |
| Occurs when the mouse button is released on the element. |
| Occurs when the pointer is over the element. |
| Occurs when the pointer moves over the element. |
| Occurs when the pointer leaves the element. |
| Occurs when the element has loaded. |
SVG document attributes apply only to the graphic’s <svg> tag and have no effect elsewhere.
Table: Event attributes for the SVG document.
Attribute | Meaning |
|---|---|
| Occurs when the SVG graphic is unloaded. |
| Occurs when loading is aborted. |
| Occurs when an error occurs. |
| Occurs when the graphic is resized. |
| Occurs when scrolling within the graphic. |
| Occurs when zooming within the graphic. |
Table: Event attributes for animations.
Attribute | Meaning |
|---|---|
| Occurs when an animation starts. |
| Occurs when the animation ends. |
| Occurs when the animation repeats. |
SVG Viewer does not yet support every standard event. The attributes onresize, onscroll, onfocusin, onfocusout, onactivate, onabort, onerror, onbegin, onend and onrepeat are currently unsupported.
The event onunload of the <svg> tag works correctly only in Internet Explorer. Adobe attributes this to a Netscape Navigator bug rather than deficient plug-in support.
Usually pass the triggering element when using these attributes, especially to modify it. Pass the called function a parameter named evt with the value evt, as previous examples demonstrated.
Note:
The value evt represents an object, comparable to JavaScript’s keyword this or VBScript’s me. It returns the object whose event handler contains it.
The second way to handle events in SVG documents is to define event listeners: functions responding independently to events, without adding event attributes to tags.
The circle-changing-color example becomes the following. First access the SVG document’s document object to implement handlers. An onload attribute on the <svg> tag must call an initialization function, here init. The script declares two global variables, elem and svgdok, initialized in the function init. The variable svgdok receives through svgdok=evt.getTarget().getOwnerDocument(); the SVG document’s document object, the graphic’s parent object. Then the getElementById method of the document object retrieves the target element, here the one with id "kreis".
Create the handler with the method addEventListener, passing three parameters: the quoted event name, the function to execute, and a third value. The function already used in hovern.svg is named farbeAendern. Supply its name without parameters or quotes as the second value. The third parameter is false.
SVG documentation does not explain this value, though examples use it. Omission causes a syntax error; replacing it with true prevents execution when the event occurs. It may be reserved for future use.
These lines make the function farbeAendern run when the events onmouseover and onmouseout occur. No more is needed; the attributes onmouseover and onmouseout can be omitted from the <circle> tag.
<?xml version="1.0"?>
...
<svg width="300" height="800" onload="init(evt)">
<script type="text/ecmascript">
<![CDATA[
var elem;
var svgdok;
function init(evt)
{
svgdok=evt.getTarget().getOwnerDocument();
elem=svgdok.getElementById("kreis");
elem.addEventListener("mouseover", farbeAendern, false)
elem.addEventListener("mouseout", farbeAendern, false)
}
function farbeAendern(evt)
{
var kreis= evt.getTarget();
var aktFarbe = kreis.getAttribute("style");
if (aktFarbe == "fill:red")
kreis.setAttribute("style", "fill:blue");
else
kreis.setAttribute("style", "fill:red");
}
]]>
</script>
<a xlink:href="#animieren">
<circle id="kreis" cx="100" cy="100" r="20" style="fill:red">
<animate id="animieren" attributeName="r" begin="indefinite" from="20" to="1" by="-1" dur="5s" fill="freeze"/>
</circle>
</a>
</svg>
This approach takes more effort for small applications but pays off with many events. Code is clearer and can handle events occurring when scripts add elements. Handlers can also be removed when needed.
Event names passed to the addEventListener method differ slightly from attribute names. For mouse events, remove the leading on. Instead of the attribute onmouseover pass the method mouseover.
Events of the <svg> tag and special DOM events differ more or lack attribute equivalents. The table lists available names.
Table: Event names for SVG event listeners.
Mouse events | SVG document events | DOM events | Animation events |
|---|---|---|---|
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| |
Copyright © verlag moderne industrie GmbH 2001
You may print the online version for your private use.
Otherwise, this chapter from “Das Einsteigerseminar SVG — Webgrafiken mit XML” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
verlag moderne industrie GmbH, Justus-von-Liebig-Straße 1, D-86899 Landsberg/Lech, fon ++49 (0) 81 91 / 125 – 0, leserservice(at)mi-verlag.de
back to top