简体   繁体   中英

How can I link JavaScript to SVG?

I have a project that works with SVG and JS. Nowadays the SVG is responsible for calling the JS. In the middle of the SVG code there is an instruction like that:

planta30263.svg

<?xml version="1.0" encoding="UTF-8"?>
....
<script type="text/javascript" xlink:href="tatu1/svgEditarVisualizacao.js"     xlink:actuate="onLoad" xlink:show="other" xlink:type="simple" />
.....

However sometimes it's necessary to change the directory from one server to another. (tatu1 -> tatu2) So, I'd like to know if there is a way to make JS calls the SVG in the page which SVG is embedded, an external calling.

vis_rota.jsp

....
<embed  src="<%=planta%>" id="plantaSVG" type="image/svg+xml" name="plantaSVG" />
....

The xlink:actuate, xlink:show and xlink:type attributes on the script element are not necessary, you can safely remove them.

It's better to use <object> rather than <embed> because then you can then provide fallback content if a browser lacks the ability to display svg, or if it has been disabled for any reason. For more information see the SVG Primer .

If you want to call functions from the svg to the document that referenced it, then see scripting between svg and html for some examples.

Not sure what you want and a plug but at iscriptdesign you can see some javascript manupulation to view different svg images. Adapt images online, and upload them using html 5's file api.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM