In most browsers, the following would work.
window.onload = function(){
console.log( document.getElementById('svgElm').getBoundingClientRect().width );
};
Here is a demo. If you try it in Google Chrome, the console will output 200
. However, FireFox returns 0
.
The solution I found for this was to use
.getComputedStyle()
. And sincesvg
elements are not supported in old IE8- browsers,.getComputedStyle()
is the way to give consistent results.So I ended up using this function in my library:
This Firefox bug was fixed in Firefox 33 which was released on 14 October 2014.
See bug 530985 for details.
I've ended up falling back to the parent dimensions if SVG properties cannot be returned. Here is a demo http://jsbin.com/uzoyik/1/edit.
The relavent code is:
I don't think "width" is a standard cross-browser property of the object returned by the getBoundingClientRect method. I typically do something like: