files/en-us/web/svg/reference/element/script/index.md
The <script> SVG element allows to add scripts to an SVG document.
[!NOTE] While SVG's
scriptelement is equivalent to the HTML {{HTMLElement('script')}} element, it has some discrepancies, like it uses the {{SVGAttr('href')}} attribute instead ofsrcand it doesn't support ECMAScript modules so far (See browser compatibility below for details)
{{svginfo}}
crossorigin
?; Animatable: yeshigh
low
auto
<media-type>; Default value: application/ecmascript; Animatable: noThis element implements the {{domxref("SVGScriptElement")}} interface.
Click the circle to change colors.
<svg
viewBox="0 0 10 10"
height="120px"
width="120px"
xmlns="http://www.w3.org/2000/svg">
<circle cx="5" cy="5" r="4" />
<script>
// <![CDATA[
function getColor() {
const R = Math.round(Math.random() * 255)
.toString(16)
.padStart(2, "0");
const G = Math.round(Math.random() * 255)
.toString(16)
.padStart(2, "0");
const B = Math.round(Math.random() * 255)
.toString(16)
.padStart(2, "0");
return `#${R}${G}${B}`;
}
document.querySelector("circle").addEventListener("click", (e) => {
e.target.style.fill = getColor();
});
// ]]>
</script>
</svg>
{{EmbedLiveSample('Example', 150, '100%')}}
{{Specifications}}
{{Compat}}
script element in HTML