0

Please understand this question properly before marking as duplicate.

There are many questions like this one in StackOverflow, but my question is slightly different from those.

I have a chakra.svg file, whose code is this:

<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" id="b">
    <circle cx="50" cy="50" r="45" stroke-width="2" stroke="blue" fill="none"/>
    <script>
        <![CDATA[
            for(i=0;i<24;i++)
            {
                var l=document.createElementNS("http://www.w3.org/2000/svg","line");
                l.setAttributeNS(null,"x1","50");
                l.setAttributeNS(null,"y1","50");
                l.setAttributeNS(null,"x2","5");
                l.setAttributeNS(null,"y2","50");
                l.setAttributeNS(null,"stroke-width",2);
                l.setAttributeNS(null,"stroke","blue");
                l.setAttributeNS(null,"transform","rotate("+(i/24*360)+",50,50)");
                document.querySelector("#b").appendChild(l);
            }
        ]]>
    </script>
</svg>

The output is rendered as expected, like this: enter image description here

I have another file, India_flag.svg whose code is this:

<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" width="170" height="100">
    <rect x="0" y="0" height="33" width="180" fill="#f93"/>
    <rect x="0" y="33" height="34" width="180" fill="white"/>
    <rect x="0" y="67" height="33" width="180" fill="green"/>
</svg>

The output of this file is also rendered as expected: enter image description here

But, now the problem is, when I am trying to insert the chakra.svg file inside the India_flag.svg file using the image tag like this:

<image xlink:href="chakra.svg" x="70" y="35" height="30" width="30"/>

The output should have been that the wheel is placed at the centre of the flag, but I am getting this output: enter image description here

The chakra.svg file is rendered, but the JavaScript code in that file did not run, only the circle is rendered. What am I doing wrong and how to achieve my objective?

2
  • Have you defined (or imported) your javascript before your code? :-) Commented Mar 29, 2019 at 6:15
  • How to do that? Commented Mar 29, 2019 at 6:19

1 Answer 1

1

SVG documents when loaded inside an <image> tag (or HTML <img> for that matter) are sand-boxed and won't allow the execution of scripts, nor will they be interactive (i.e no user-event).

To circumvent that, you need an other way to append you svg. The simple solution would be to copy its markup directly in the main one:

<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" width="170" height="100">
  <rect x="0" y="0" height="33" width="180" fill="#f93"/>
  <rect x="0" y="33" height="34" width="180" fill="white"/>
  <rect x="0" y="67" height="33" width="180" fill="green"/>
  <svg x="70" y="35" height="30" width="30" id="b" viewBox="0 0 100 100">
    <circle cx="50" cy="50" r="45" stroke-width="2" stroke="blue" fill="none"/>
    <script>
      <![CDATA[
        for(i=0;i<24;i++) {
          var l=document.createElementNS("http://www.w3.org/2000/svg","line");
          l.setAttribute("x1","50");
          l.setAttribute("y1","50");
          l.setAttribute("x2","5");
          l.setAttribute("y2","50");
          l.setAttribute("stroke-width",2);
          l.setAttribute("stroke","blue");
          l.setAttribute("transform","rotate("+(i/24*360)+",50,50)");
          document.querySelector("#b").appendChild(l);
        }
      ]]>
    </script>
  </svg>
</svg>

An other solution, would have been to do the inverse: load your flag image from the one with the script: plnkr.

Sign up to request clarification or add additional context in comments.

7 Comments

Yes that was a bad idea sorry use don't allow script execution either, edited since then.
@Ruturaj said something about importing JavaScript before the code. What is it and how to do that?
No offense whatsoever, but I fear they simply don't know what they are talking about and thrown some random words in a sentence.
So, is there no way of doing what I want, that is, including a SVG document inside another SVG document along with the running JavaScript code?
Well the code in my answer does that. It could be automated by js if you need. And iIf you absolutely need it to be an external svg, then you'd have to resort on using a <foreignObject> that would host an html <iframe> that would load your file.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.