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.
| Categories | None |
|---|---|
| Permitted content | Any elements or character data |
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:
This element implements the SVGScriptElement interface.
| Specification | Status | Comment |
|---|---|---|
| Scalable Vector Graphics (SVG) 2 The definition of '<script>' in that specification. | Candidate Recommendation | |
| Scalable Vector Graphics (SVG) 1.1 (Second Edition) The definition of '<script>' in that specification. | Recommendation | Initial definition |
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | 1 | Yes | 1.5 | 9 | 9 | 3.1 |
type |
? | ? | ? | ? | ? | ? |
| xlink:href | ? | ? | ? | ? | ? | ? |
| Mobile | |||||||
|---|---|---|---|---|---|---|---|
| Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | 3 | 18 | Yes | 4 | Yes | 3.1 | ? |
type |
? | ? | ? | ? | ? | ? | ? |
| xlink:href | ? | ? | ? | ? | ? | ? | ? |
© 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