I have an SVG image file and I want to put it to in HTML page as an SVG. So I still take the advantage of Zoom in/out with high resolution.
Here is my code. I put the SVG inside the , the inside the SVG.
The code run correctly but no SVG appear in the browser.
1) How can I show it? 2) Is there any way to place the SVG as SVG with all of its features( I read some question but non of them work with me).
// this to draw the svg
var div = document.createElement("div");
div.id="dsvg";
div.class="cdsvg";
div.style.width = "auto";
div.style.height = "210px";
var link='SVGimage.svg';
//creat svg to embed the svg to them
var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute("height", 210);
svg.setAttribute("width", 500);
//svg.setAttribute('xlink:href', link );
var XLink_NS = 'http://www.w3.org/1999/xlink';
var img = document.createElementNS(svg, 'image');
img.setAttributeNS(null, 'height', '210');
img.setAttributeNS(null, 'width', '500');
img.setAttributeNS(XLink_NS, 'xlink:href', link);
svg.appendChild(img);
var cell3 = row.insertCell(3);
div.appendChild(svg);
cell3.appendChild(div);
This HTML code is work but when I transfer it to JavaScript it does not ..
<svg id="svgimg" height="60" width="60" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
UPDATE: I can see the SVG as img now: I added the library in order to change to SVG( because I want to change the colour of the lines and rectangle inside the SVG )
var link='test.svg';
var svgframe = document.createElement('img');
svgframe.id="svgframe";
svgframe.class="svg";
svgframe.setAttribute('src',link );
var SVGs = document.querySelectorAll('.svg');
SVGInjector(SVGs);
but when I see the inspect it is still img tag not SVG?? I want it as SVG so I can change the colour of the rectangles and the lines
after our conversation in the comment, I think I can give you some help. Everything you need to know is here, Iconic does a huge work on SVGs and opensource it.
Let's say you have icon.svg :
You can add it in your html like that:
And you can use SVG Injector
So you will be replaced by the SVG code. Than, you can style it
This line is incorrect
You have the namespace right for the svg element
so you just need to do the same again for the image i.e.
Use parameters: http://localhost/circle.svg?color=blue
Use inline CSS:
Somebody also mentioned this but using with encodeURI... https://stackoverflow.com/a/42313650/1347572
It looks like you're trying to dynamically create your SVG element and then have it display in the browser.
Here's a rough cut of how I've done it in the past. This solution uses jQuery's html() function:
More precisely, if you wanted to place the SVG at a specific point in the document, such as the div
myDiv
:$('#myDiv').html(svgdiv.html());