SVG translate transform in javascript not working in IE 11 - javascript

The following code doesn't work in IE 11 (Chrome works fine)
<html>
<head>
<script>
window.onload = function() {document.getElementById("abc").style.transform = "translate(100px,100px)";};
</script>
</head>
<body>
<div>
<svg width="200" height="200">
<g id="abc">
<polygon points="14,7 0,14 0,0"></polygon>
</g>
</svg>
</div>
</body>

For IE you need to set transform as an attribute rather than as a CSS style.
Note that for an attribute, units are not allowed.
<html>
<head>
<script>
window.onload = function() {document.getElementById("abc").setAttribute("transform", "translate(100, 100)")};
</script>
</head>
<body>
<div>
<svg width="200" height="200">
<g id="abc">
<polygon points="14,7 0,14 0,0"></polygon>
</g>
</svg>
</div>
</body>

Related

How can I loop through path elements in SVG?

I am getting TypeError: logo[i].getTotalLength is not a function error while trying to mesure the length of each <path> of the <svg> tag!
I got that error using jquery :
jQuery('#logo').each(function(i) {
console.log( i + ' ====' + jQuery(this).get(0).getTotalLength());
});
So I rewrote the same code in javascript but I still get the same error!
javascript code :
const logo= document.querySelectorAll("#logo");
for (let i=0; i < logo.length; i++)
{
console.log(i," ==== ",logo[i].getTotalLength());
}
Note: in this html code there is only 1 <path> , I removed the others because they are very large!
html code :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body style="background-color: black;">
<svg id="logo" width="80" height="90" viewBox="0 0 80 90" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M32.56 3.24001C41.36 3.24001 49.08 4.96001 55.72 8.40001C62.44 11.84 67.6 16.76 71.2 23.16C74.88 29.48 76.72 36.84 76.72 45.24C76.72 53.64 74.88 61 71.2 67.32C67.6 73.56 62.44 78.4 55.72 81.84C49.08 85.28 41.36 87 32.56 87H3.28V3.24001H32.56ZM31.96 72.72C40.76 72.72 47.56 70.32 52.36 65.52C57.16 60.72 59.56 53.96 59.56 45.24C59.56 36.52 57.16 29.72 52.36 24.84C47.56 19.88 40.76 17.4 31.96 17.4H20.08V72.72H31.96Z" stroke="white" stroke-width="6"/>
</svg>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="app.js"></script>
</body>
</html>
When i removed id="logo" from the <svg> tag and add it to the <path> it work perfectly fine!
So the loop doesn't work properly!
If you want to loop through all the elements with a particular tag name then there's a function that will get all of them: getElementsByTagName.
You were getting the parent <svg> element and there's only one of them. You could iterate through its children and find the path elements but why bother given there's an easier way to do it.
const logo= document.getElementsByTagName("path");
for (let i=0; i < logo.length; i++)
{
console.log(i," ==== ",logo[i].getTotalLength());
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body style="background-color: black;">
<svg id="logo" width="80" height="90" viewBox="0 0 80 90" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M32.56 3.24001C41.36 3.24001 49.08 4.96001 55.72 8.40001C62.44 11.84 67.6 16.76 71.2 23.16C74.88 29.48 76.72 36.84 76.72 45.24C76.72 53.64 74.88 61 71.2 67.32C67.6 73.56 62.44 78.4 55.72 81.84C49.08 85.28 41.36 87 32.56 87H3.28V3.24001H32.56ZM31.96 72.72C40.76 72.72 47.56 70.32 52.36 65.52C57.16 60.72 59.56 53.96 59.56 45.24C59.56 36.52 57.16 29.72 52.36 24.84C47.56 19.88 40.76 17.4 31.96 17.4H20.08V72.72H31.96Z" stroke="white" stroke-width="6"/>
</svg>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="app.js"></script>
</body>
</html>

Unable to change svg colour using jQuery

I'm having trouble changing the colour of an svg image with jquery or vanila javascript. I've tried a couple of things but nothing seems to work, the original colour is black and i can change it if i open the svg image in my IDE and add/change the fill attribute. But not with JS. What am I doing wrong? Other lines of code work fine so it is not a problem with the files or so.
So far i've tried these lines of codes and put id on the img tag aswell as the svg element it self
html:
<img src="import/splash.svg">
JS:
$('#myId').css({fill:"#f8b9d4"});
$("myId").attr("fill", "yellow");
document.getElementById("myId").setAttribute("fill", "#f8b9d4");
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="100%" viewBox="0 0 588 497" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<title>random title</title>
<g>
<path
d="M259.952694,496.613554 C253.376792,496.613554 250.627732,488.109987 250.038987,483.084288 L246.271494,450.855246 C245.492831,444.196255 243.408484,440.818093 240.070679,440.818093 C236.607054,440.818093 232.400376,443.96598 231.070952,445.174331 L199.957676,473.559913 C195.010319,478.077582 191.435117,480.178547 188.70505,480.178547 C187.190455,480.178547 185.884771,479.528079 185.020645,478.350589 C182.383162,474.756395 184.650306,466.661151 185.407603,464.263439 L192.935467,440.450127 C193.543204,438.531958 194.03699,435.934833 192.949711,434.446352 C191.893294,433.002977 189.478964,432.791694 188.135296,432.791694 C187.140602,432.791694 186.411793,432.903271 186.3382,432.91514 L127.318884,443.085236 C118.696618,444.573716 112.519544,442.940424 109.001317,438.313553 C103.842676,431.523993 106.712808,420.883851 106.838629,420.439918 "
id="myId" fill=""></path>
</g>
UPDATE - created a completley new project but the problem is the same. (The div is for testing and working fine)
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="tmo.js"></script>
</head>
<body>
<img src="splash.svg" alt="">
<div id="h"></div>
</body>
</html>
jquery:
$(document).ready(function () {
$('#myId').css({fill:"blue"});
$("#h").text("heasdadasdsadasdsad");
});
SVG file:
<svg width="100%" viewBox="0 0 588 497" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<title>random title</title>
<g>
<path
d="M259.952694,496.613554 C253.376792,496.613554 250.627732,488.109987 250.038987,483.084288 L246.271494,450.855246 C245.492831,444.196255 243.408484,440.818093 240.070679,440.818093 C236.607054,440.818093 232.400376,443.96598 231.070952,445.174331 L199.957676,473.559913 "
id="myId"></path>
</g>
</svg>
I'd say it's because you're using an <img> tag rather than putting the <svg> directly in the HTML.
If you cannot put the <svg> directly in your HTML, embed it using the <object> tag, e.g. like this:
<object id="img" data="test.svg" type="image/svg+xml">Your browsser doesn't support SVG</object>
Then, you can access elements inside the <object> like this:
document.getElementById("img").contentDocument.getElementById("myId").setAttribute("fill", "#f8b9d4");
Your Jquery css works fine. You don't need to have an empty fill="" attribute on the path.
Note: Scroll down on the code snippet to see result, your viewbox is a bit big :)
$('#myId').css({fill:"blue"});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg width="100%" viewBox="0 0 588 497" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<title>random title</title>
<g>
<path
d="M259.952694,496.613554 C253.376792,496.613554 250.627732,488.109987 250.038987,483.084288 L246.271494,450.855246 C245.492831,444.196255 243.408484,440.818093 240.070679,440.818093 C236.607054,440.818093 232.400376,443.96598 231.070952,445.174331 L199.957676,473.559913 C195.010319,478.077582 191.435117,480.178547 188.70505,480.178547 C187.190455,480.178547 185.884771,479.528079 185.020645,478.350589 C182.383162,474.756395 184.650306,466.661151 185.407603,464.263439 L192.935467,440.450127 C193.543204,438.531958 194.03699,435.934833 192.949711,434.446352 C191.893294,433.002977 189.478964,432.791694 188.135296,432.791694 C187.140602,432.791694 186.411793,432.903271 186.3382,432.91514 L127.318884,443.085236 C118.696618,444.573716 112.519544,442.940424 109.001317,438.313553 C103.842676,431.523993 106.712808,420.883851 106.838629,420.439918 "
id="myId"></path>
</g>
</svg>
Try This-
$('#myId').css({fill:"blue"});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg width="100%" viewBox="0 0 588 497" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<title>random title</title>
<g>
<path
d="M259.952694,496.613554 C253.376792,496.613554 250.627732,488.109987 250.038987,483.084288 L246.271494,450.855246 C245.492831,444.196255 243.408484,440.818093 240.070679,440.818093 C236.607054,440.818093 232.400376,443.96598 231.070952,445.174331 L199.957676,473.559913 C195.010319,478.077582 191.435117,480.178547 188.70505,480.178547 C187.190455,480.178547 185.884771,479.528079 185.020645,478.350589 C182.383162,474.756395 184.650306,466.661151 185.407603,464.263439 L192.935467,440.450127 C193.543204,438.531958 194.03699,435.934833 192.949711,434.446352 C191.893294,433.002977 189.478964,432.791694 188.135296,432.791694 C187.140602,432.791694 186.411793,432.903271 186.3382,432.91514 L127.318884,443.085236 C118.696618,444.573716 112.519544,442.940424 109.001317,438.313553 C103.842676,431.523993 106.712808,420.883851 106.838629,420.439918 "
id="myId"></path>
</g>
</svg>

Dynamically replace a text element of an svg object

I would like to dynamically replace a text element of an svg object from a local text file. Below my files text et svg:
text file:
<p id="p1">It works fine</p>
text svg:
<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
<rect id="svg_1" height="87" width="162" y="188" x="130" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="5" stroke="blue" fill="yellow"/>
<text xml:space="preserve" text-anchor="start" font-family="serif" font-size="24" id="divA" y="240" x="313" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" stroke="#000000" fill="blue"> </text>
</svg>
It works fine with the div or if I put the value, for example dog, for the element svg to modify. below mon fichier html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var x = $("#divA").load("demo_test.txt #p1");
var y = $("#div1").load("demo_test.txt #p1");
document.getElementById("div1").textContent = y;
var svg = document.getElementById("textsvg");
var svgDoc = svg.contentDocument;
svgDoc.getElementById("divA").textContent = "dog";
});
</script>
</head>
<body>
<div id="div1"><h2>PSU0 to PDU-A G0-2</h2></div>
<object id="textsvg" width="600" height="480" type="image/svg+xml" data="essai.svg"></object>
</body>
However, when I put the variable y for the text element svg I have the message "[object Object]" instead of the message "It works fine". Below the html file:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var x = $("#divA").load("demo_test.txt #p1");
var y = $("#div1").load("demo_test.txt #p1");
document.getElementById("div1").textContent = y;
var svg = document.getElementById("textsvg");
var svgDoc = svg.contentDocument;
svgDoc.getElementById("divA").textContent = x;
});
</script>
</head>
<body>
<div id="div1"><h2>PSU0 to PDU-A G0-2</h2></div>
<object id="textsvg" width="600" height="480" type="image/svg+xml" data="essai.svg"></object>
</body>
Can you help me.
Thanks,
B.R,
<script>
$(document).ready(function(){
$("#divA").html("Gorillaz Inc");
});
</script>
This works with jquery
Full example: http://codepen.io/anon/pen/pRRgaP
Note: I didn't use files, I just updated the svg dynamically
I solved part of my problem. I have directly embedded the "svg" into my html.
I noticed that by downloading the text file without the tags: demo_text1.txt the div appears well and the text svg:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#div1").load("demo_test.txt #p2");
$("#divA").load("demo_test1.txt");
});
</script>
</head>
<body>
<div id="div1"><h2 style="color:blue">PSU0 to PDU-A G0-2</h2></div>
<svg width="800" height="480" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
<rect id="svg_1" height="87" width="162" y="20" x="5" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="5" stroke="blue" fill="yellow"/>
<text xml:space="preserve" text-anchor="start" font-family="serif" font-size="14" id="divA" y="67" x="175" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" stroke="#000000" fill="blue"> </text>
</svg>
</body>
</html>
fichier text:demo_test.txt
<p id="p1" >It works fine</p>
<p id="p2" style="color:blue">I use the local text file "demo_test.txt".The "div" works fine with tags</p>
fichier text:demo_text1.txt
I use the local text file "demo_test1.txt".
The text of svg works fine without tag but doesn't work with tags
Screen display ok:
enter image description here
However, by pointing the text svg to the text file with the tags (demo_test.txt, it does not work:
Screen not ok:
enter image description here

javascript not working in embed svg file

I just start learning SVG. got this example code from Manipulating SVG Documents Using ECMAScript (Javascript) and the DOM. I change it a little:
<!DOCTYPE html>
<html>
<body>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="300">
<script type="text/ecmascript">
<![CDATA[
function changeRectColor(evt) {
var red = Math.round(Math.random() * 255);
evt.target.setAttributeNS(null,"fill","rgb("+ red +","+ red+","+red+")");
}
]]>
</script>
<g id="firstGroup">
<rect id="myBlueRect" width="100" height="50" x="40" y="20" fill="blue" onclick="changeRectColor(evt)"/>
<text x="40" y="100">Click on rectangle to change it's color.</text>
</g>
</svg>
</body>
</html>
It works just fine. I move to separated SVG file, then js code stop working:
<!DOCTYPE html>
<html>
<body>
<object type="image/svg+xml" data="exampl3a.svg" />
<script type="text/ecmascript">
<![CDATA[
function changeRectColor(evt) {
var red = Math.round(Math.random() * 255);
evt.target.setAttributeNS(null,"fill","rgb("+ red +","+ red+","+red+")");
}
]]>
</script>
</body>
</html>
SVG file: exampl3a.svg
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="300">
<g id="firstGroup">
<rect id="myBlueRect" width="100" height="50" x="40" y="20" fill="blue" onclick="changeRectColor(evt)"/>
<text x="40" y="100">Click on rectangle to change it's color.</text>
</g>
</svg>
what should I do?
Thanks
Wes
If you put svg into a different file, then it will be in another document, and you'll need to bind to that document, using getSVGDocument. And yes, this will still not work in Chrome for local files (only for the website, or unless Chrome is run with corresponding command-line switch).
SVG:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="300">
<g id="firstGroup">
<rect id="myBlueRect" width="100" height="50" x="40" y="20" fill="blue" />
<text x="40" y="100">Click on rectangle to change it's color.</text>
</g>
</svg>
HTML
<!DOCTYPE html>
<html>
<body>
<object id='mySvg' type="image/svg+xml" data="example3a.svg" />
<script type="text/ecmascript">
function changeRectColor(evt) {
var red = Math.round(Math.random() * 255);
evt.target.setAttributeNS(null,"fill","rgb("+ red +","+ red+","+red+")");
}
var obj = document.getElementById('mySvg');
obj.addEventListener('load', function() {
var svgDoc= obj.getSVGDocument();
var elem = svgDoc.getElementById("myBlueRect");
elem.addEventListener('click', changeRectColor);
});
</script>
</body>
</html>
This was my experience: It getSVGDocument works on a server (Github worked), but not for local files. If you have an older computer (Windows XP) it will work on that, too.

JQuery load (on an svg) into HTML div isn't working

<!DOCTYPE html>
<!-- initial rendition -->
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(function () {
$('#bat').load('sample.svg');
});
</script>
</head>
<body>
<div id="bat">
<!--
<svg height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>
-->
</div>
</body>
</html>
I wrote this short code to test it and it's still not working. The contents of "sample.svg" is the commented out svg tag in #bat. When uncommented the svg in #bat displays just fine.
"sample.svg" is in the same directory as this html.
Any help is appreciated.

Categories