How to get a working dynamic javascript reference in an SVG -


what trying set javascript script reference in embedded svg document dynamically javascript code within html.

i assumed easy dynamically appending node appropriate place in embedded svg document.

i found node added @ runtime (can check firefox inspect element), javascript code contained in reference not executed when needs be.

anybody have idea this? else need done svg able call functions in javascript code?

here simplified scenario:

test.html (embeds svg)

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script type="text/javascript">     function activatescript() {       var svgdoc = document.getelementbyid("svg");       var newref = svgdoc.contentdocument.createelement('script');       newref.setattribute("type", "text/javascript");       newref.setattribute("xlink:href", "script.js");       svgdoc.contentdocument.documentelement.appendchild(newref);     } </script> </head> <body> <input type="button" value="activate script" onclick="activatescript()" /> <object id="svg" type="image/svg+xml" data="embed.svg"/> </body> </html> 

embed.svg

<?xml version="1.0"?> <svg xmlns="http://www.w3.org/2000/svg" width="12cm" height="12cm" xmlns:xlink="http://www.w3.org/1999/xlink"> <g style="fill-opacity:1; stroke:black; stroke-width:0.1cm;"> <circle cx="6cm" cy="2cm" r="100" style="fill:red;" transform="translate(0,50)" onclick="myfunction()"/> </g> </svg> 

and external javascript trying dynamically assign (script.js)

function myfunction() { alert("i can execute script!"); } 

svg elements must created in svg namespace http://www.w3.org/2000/svg instead of createelement must use createelementns xlink attribute must go in xlink namespace need

  var newref = svgdoc.contentdocument.createelementns('http://www.w3.org/2000/svg', 'script');   newref.setattribute("type", "text/javascript");   newref.setattributens("http://www.w3.org/1999/xlink", "xlink:href", "script.js"); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -