SVG 1.1 (Second Edition) Ä�ā‚¬ā€� 16 August 2011Top Ä�ā€¹ā€¦ Contents Ä�ā€¹ā€¦ Previous Ä�ā€¹ā€¦ Next Ä�ā€¹ā€¦ Elements Ä�ā€¹ā€¦ Attributes Ä�ā€¹ā€¦ Properties

18 Scripting

Contents

18.1 Specifying the scripting language

18.1.1 Specifying the default scripting language

The Ä�ā‚¬ļæ½contentScriptTypeÄ�ā‚¬ā„¢ attribute on the Ä�ā‚¬ļæ½svgÄ�ā‚¬ā„¢ element specifies the default scripting language for the given document fragment.

contentScriptType = "content-type"
Identifies the default scripting language for the given SVG document fragment. This attribute sets the default scripting language used to process the value strings in event attributes. This language must be used for all instances of script that do not specify their own scripting language. The value content-type specifies a media type, per MIME Part Two: Media Types [RFC2046]. The default value is 'application/ecmascript' [RFC4329].
Animatable: no.

18.1.2 Local declaration of a scripting language

It is also possible to specify the scripting language for each individual Ä�ā‚¬ļæ½scriptÄ�ā‚¬ā„¢ element by specifying a Ä�ā‚¬ļæ½typeÄ�ā‚¬ā„¢ on the Ä�ā‚¬ļæ½scriptÄ�ā‚¬ā„¢ element.

18.2 The Ä�ā‚¬ļæ½scriptÄ�ā‚¬ā„¢ element

A Ä�ā‚¬ļæ½scriptÄ�ā‚¬ā„¢ element is equivalent to the Ä�ā‚¬ļæ½scriptÄ�ā‚¬ā„¢ element in HTML and thus is the place for scripts (e.g., ECMAScript). Any functions defined within any Ä�ā‚¬ļæ½scriptÄ�ā‚¬ā„¢ element have a "global" scope across the entire current document.

Example script01 defines a function circle_click which is called by the Ä�ā‚¬ļæ½onclickÄ�ā‚¬ā„¢ event attribute on the Ä�ā‚¬ļæ½circleÄ�ā‚¬ā„¢ element. The drawing below on the left is the initial image. The drawing below on the right shows the result after clicking on the circle.

Note that this example demonstrates the use of the Ä�ā‚¬ļæ½onclickÄ�ā‚¬ā„¢ event attribute for explanatory purposes. The example presupposes the presence of an input device with the same behavioral characteristics as a mouse, which will not always be the case. To support the widest range of users, the Ä�ā‚¬ļæ½onactivateÄ�ā‚¬ā„¢ event attribute should be used instead of the Ä�ā‚¬ļæ½onclickÄ�ā‚¬ā„¢ event attribute.

Before attempting to execute the Ä�ā‚¬ļæ½scriptÄ�ā‚¬ā„¢ element the resolved media type value for Ä�ā‚¬ļæ½typeÄ�ā‚¬ā„¢ must be inspected. If the SVG user agent does not support the scripting language then the Ä�ā‚¬ļæ½scriptÄ�ā‚¬ā„¢ element must not be executed.

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="6cm" height="5cm" viewBox="0 0 600 500"
     xmlns="http://www.w3.org/2000/svg" version="1.1">
  <desc>Example script01 - invoke an ECMAScript function from an onclick event
  </desc>
  <!-- ECMAScript to change the radius with each click -->
  <script type="application/ecmascript"> <![CDATA[
    function circle_click(evt) {
      var circle = evt.target;
      var currentRadius = circle.getAttribute("r");
      if (currentRadius == 100)
        circle.setAttribute("r", currentRadius*2);
      else
        circle.setAttribute("r", currentRadius*0.5);
    }
  ]]> </script>

  <!-- Outline the drawing area with a blue line -->
  <rect x="1" y="1" width="598" height="498" fill="none" stroke="blue"/>

  <!-- Act on each click event -->
  <circle onclick="circle_click(evt)" cx="300" cy="225" r="100"
          fill="red"/>

  <text x="300" y="480" 
        font-family="Verdana" font-size="35" text-anchor="middle">

    Click on circle to change its size
  </text>
</svg>
Example script01
Example script01 Ä�ā‚¬ā€¯ invoke an ECMAScript function from an onclick event Ä�ā‚¬ā€¯ before first clickÄ€Ā Example script01 Ä�ā‚¬ā€¯ invoke an ECMAScript function from an onclick event Ä�ā‚¬ā€¯ after first click

View this example as SVG (SVG-enabled browsers only)

Ä�ā‚¬ļæ½scriptÄ�ā‚¬ā„¢
Categories:
None
Content model:
Any elements or character data.
Attributes:
DOM Interfaces:

Attribute definitions:

type = "content-type"
Identifies the scripting language for the given Ä�ā‚¬ļæ½scriptÄ�ā‚¬ā„¢ element. The value content-type specifies a media type, per Multipurpose Internet Mail Extensions (MIME) Part Two [RFC2046]. If a Ä�ā‚¬ļæ½typeÄ�ā‚¬ā„¢ is not provided, the value of Ä�ā‚¬ļæ½contentScriptTypeÄ�ā‚¬ā„¢ on the Ä�ā‚¬ļæ½svgÄ�ā‚¬ā„¢ element shall be used, which in turn defaults to "application/ecmascript" [RFC4329]. If a Ä�ā‚¬ļæ½scriptÄ�ā‚¬ā„¢ element falls outside of the outermost svg element and the Ä�ā‚¬ļæ½typeÄ�ā‚¬ā„¢ is not provided, the Ä�ā‚¬ļæ½typeÄ�ā‚¬ā„¢ must default to "application/ecmascript" [RFC4329].
Animatable: no.
xlink:href = "<iri>"
An IRI reference to an external resource containing the script code.
Animatable: no.

18.3 Event handling

Events can cause scripts to execute when either of the following has occurred:

Related sections of the spec:

18.4 Event attributes

The following event attributes are available on many SVG elements.

The complete list of events that are part of the SVG language and SVG DOM and descriptions of those events is provided in Complete list of supported events.

18.4.1 Event attribute for the SVGLoad event

Below is the definition for the Ä�ā‚¬ļæ½onloadÄ�ā‚¬ā„¢ event attribute. It can be specified on all of the animation elements and most of the graphics elements and container elements. The Ä�ā‚¬ļæ½onloadÄ�ā‚¬ā„¢ event attribute is classified as both a graphical event attribute and an animation event attribute. (See the definition for each element to determine whether it can have a graphical event attribute specified on it.)

Attribute definitions:

onload = "<anything>"
Specifies some script to execute when "bubbling" or "at target" phase listeners for the SVGLoad event are fired on the element the attribute is specified on.
Animatable: no.

18.4.2 Event attributes on graphics and container elements

Below are the definitions for the graphical event attributes. These can be specified on most graphics elements and container elements. (See the definition for each element to determine whether it can have a graphical event attribute specified on it.)

Note that Ä�ā‚¬ļæ½onloadÄ�ā‚¬ā„¢, defined above, is also classified as a graphical event attribute.

Attribute definitions:

onfocusin = "<anything>"
onfocusout = "<anything>"
onactivate = "<anything>"
onclick = "<anything>"
onmousedown = "<anything>"
onmouseup = "<anything>"
onmouseover = "<anything>"
onmousemove = "<anything>"
onmouseout = "<anything>"
Specifies some script to execute when "bubbling" or "at target" phase listeners for the corresponding event are fired on the element the attribute is specified on. See the Complete list of support events to determine which event each of these event attributes corresponds to.
Animatable: no.

18.4.3 Document-level event attributes

Below are the definitions for the document event attributes. These can be specified only on Ä�ā‚¬ļæ½svgÄ�ā‚¬ā„¢ elements.

Attribute definitions:

onunload = "<anything>"
onabort = "<anything>"
onerror = "<anything>"
onresize = "<anything>"
onscroll = "<anything>"
onzoom = "<anything>"
Specifies some script to execute when "bubbling" or "at target" phase listeners for the corresponding event are fired on the element the attribute is specified on. See the Complete list of support events to determine which event each of these event attributes corresponds to.
Animatable: no.

18.4.4 Animation event attributes

Below are the definitions for the animation event attributes. These can be specified on the animation elements.

Note that Ä�ā‚¬ļæ½onloadÄ�ā‚¬ā„¢, defined above, is also classified as an animation event attribute.

Attribute definitions:

onbegin = "<anything>"
onend = "<anything>"
onrepeat = "<anything>"
Specifies some script to execute when "bubbling" or "at target" phase listeners for the corresponding event are fired on the element the attribute is specified on. See the Complete list of support events to determine which event each of these event attributes corresponds to.
Animatable: no.

18.5 DOM interfaces

18.5.1 Interface SVGScriptElement

The SVGScriptElement interface corresponds to the Ä�ā‚¬ļæ½scriptÄ�ā‚¬ā„¢ element.
interface SVGScriptElement : SVGElement,
                             SVGURIReference,
                             SVGExternalResourcesRequired {
  attribute DOMString type setraises(DOMException);
};
Attributes:
type (DOMString)
Corresponds to attribute Ä�ā‚¬ļæ½typeÄ�ā‚¬ā„¢ on the given Ä�ā‚¬ļæ½scriptÄ�ā‚¬ā„¢ element.
Exceptions on setting
DOMException, code NO_MODIFICATION_ALLOWED_ERR
Raised on an attempt to change the value of a read only attribute.

18.5.2 Interface SVGZoomEvent

A DOM consumer can use the hasFeature of the DOMImplementation interface to determine whether the SVG zoom event set has been implemented by a DOM implementation. The feature string for this event set is "SVGZoomEvents". This string is also used with the createEvent method.

The zoom event handler occurs before the zoom event is processed. The remainder of the DOM represents the previous state of the document. The document will be updated upon normal return from the event handler.

The UI event type for a zoom event is:

SVGZoom
The zoom event occurs when the user initiates an action which causes the current view of the SVG document fragment to be rescaled. Event handlers are only recognized on Ä�ā‚¬ļæ½svgÄ�ā‚¬ā„¢ elements. See SVGZoom event.
interface SVGZoomEvent : UIEvent {
  readonly attribute SVGRect zoomRectScreen;
  readonly attribute float previousScale;
  readonly attribute SVGPoint previousTranslate;
  readonly attribute float newScale;
  readonly attribute SVGPoint newTranslate;
};
Attributes:
zoomRectScreen (readonly SVGRect)

The specified zoom rectangle in screen units.

The SVGRect object is read only.

previousScale (readonly float)
The scale factor from previous zoom operations that was in place before the zoom operation occurred.
previousTranslate (readonly SVGPoint)

The translation values from previous zoom operations that were in place before the zoom operation occurred.

The SVGPoint object is read only.

newScale (readonly float)
The scale factor that will be in place after the zoom operation has been processed.
newTranslate (readonly SVGPoint)

The translation values that will be in place after the zoom operation has been processed.

The SVGPoint object is read only.

SVG 1.1 (Second Edition) Ä�ā‚¬ā€� 16 August 2011Top Ä�ā€¹ā€¦ Contents Ä�ā€¹ā€¦ Previous Ä�ā€¹ā€¦ Next Ä�ā€¹ā€¦ Elements Ä�ā€¹ā€¦ Attributes Ä�ā€¹ā€¦ Properties