W3cubDocs

/SVG

<script>

A SVG 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.

Usage context

Categories None
Permitted content Any elements or character data

Example

The following code snippet demonstrates the use of the SVG script tag. In this code, we use JavaScript to change the radius of the SVG <circle> element.

<svg width="100%" height="100%" viewBox="0 0 100 100"
     xmlns="http://www.w3.org/2000/svg">
  <script type="text/javascript">
    // <![CDATA[
    function change(evt) {
      var target = evt.target;
      var radius = target.getAttribute("r");

      if (radius == 15) {
        radius = 45;
      } else {
        radius = 15;
      }

      target.setAttribute("r",radius);
   }
   // ]]>
  </script>

  <circle cx="50" cy="50" r="45" fill="green"
          onclick="change(evt)" />
</svg>

Live result:

Attributes

Global attributes

Specific attributes

DOM interface

This element implements the SVGScriptElement interface.

Specifications

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 (Yes) 1.5 (1.8) 9.0 9.0 3.0.4
Feature Android Edge Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 3.0 (Yes) 1.0 (1.8) No support (Yes) 3.0.4

The chart is based on these sources.

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/SVG/Element/script