Get SVG viewBox via javascript – SOLVED!

1st. solution

var svg = document.getElementById("svg1");
var box = svg.getAttribute('viewBox');
box.split(/\s+|,/);

Result:

(4) [“0”, “0”, “900”, “50”]
0: “0”
1: “0”
2: “900”
3: “50”

2nd. a more elegant solution

var svg = document.getElementById("svg1");
var box = svg.viewBox.baseVal;

Result:

SVGRect {x: 0, y: 0, width: 900, height: 50}
height: 50
width: 900
x: 0
y: 0

byrev Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *