Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load an external JavaScript script to (pure) SVG document?

Tags:

javascript

svg

I have fairly large script within an .svg file and I'd like to get it out of the CDATA section into an external file. How to achieve this?

The HTML regular does not work:

<script type="text/javascript" src="Track_animation.js" /> 

The documentation I have been able to find only concerns with JS embedded within the SVG itself. Is use of external scripts even possible?

Using OS X and Safari 4.0.4 for rendering.

like image 786
akauppi Avatar asked Nov 25 '09 14:11

akauppi


People also ask

Can you put JavaScript in SVG?

JavaScript can be added anywhere in an SVG document between the opening and closing <svg> tags. In general, a script should be placed at the end of the document to avoid blocking and allow the script complete access to the DOM.

How is an external JavaScript loaded into a document?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Where do I put JavaScript script files?

JavaScript in <head> or <body> You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

What file type is given to external JavaScript files?

An external JavaScript file must be saved by . js extension.


2 Answers

Try this

<script xlink:href="file_name" /> 
like image 90
vsr Avatar answered Sep 17 '22 03:09

vsr


Maybe you will need to have xmlns:xlink="http://www.w3.org/1999/xlink" as attribute in your <svg ... >.

like image 36
piR Avatar answered Sep 19 '22 03:09

piR