External javascript code from svg

Discuss SVG code, accessible via the XML Editor.
katun89
Posts: 12
Joined: Sat Jan 08, 2011 12:57 pm

External javascript code from svg

Postby katun89 » Sat Jan 08, 2011 1:05 pm

Hi guys,
I got a document like this:


User avatar
tomh
Posts: 218
Joined: Sat Feb 14, 2009 10:14 pm

Re: External javascript code from svg

Postby tomh » Sun Jan 09, 2011 9:44 am

OK first of all, as you have found the embedded SVG file and the page around it do not share the same DOM, so direct access is not possible.

SVG files support the <script> tag, and this allows you to reference external .js files. Unlike HTML, it does not support a src attribute but uses XML's xlink instead:

eg:

Code: Select all

<script type="text/ecmascript" xlink:href="lib.js"></script>


Note you have to define the xlink namespace in the <svg> tag like so:

Code: Select all

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ...>


Some more details at carto.net

Tom


Return to “SVG / XML Code”