Is there a way to get svg size using javascript?
No and yes.
not
Not:
JavaScript will not be able to access the contents of the SVG file sitting in the browser.
Thus, it would be impossible to have a page containing an arbitrary SVG image, and then JavaScript would determine something from the SVG file itself.
The only JS data that can access it is on the DOM page: the original markup plus any JS related changes to the DOM. You can access the attributes of the object element and descendant nodes, but this will not give you the appearance of anything more than what you can see if you look at the page layout yourself.
Yes:
JS (in modern browsers) can parse any XML string in the DOM, and then access the content using DOM-based methods.
You can get the contents of an SVG file by issuing a request of the type xmlHttpRequest (i.e. JS performs an HTTP GET to the URL of the SVG file). After that, JS will have a full line of SVG XML file, and you can access anything.
Or maybe I should just look at the svg xml file to find out the size of the top svg tag?
That would be more likely.
The only JS-based solution will require JS to execute a GET request on the URL of the SVG file (as indicated above), which is hardly possible - each page load can double load each SVG file, and parsing SVG XML in the DOM by JS can be too resource intensive.
When JS extracts the contents of the SVG XML and parses it in the DOM to get only the image dimensions, this is a bit overkill. This is entirely possible, but generally not practical.
Querying the server side of the SVG XML content, determining the appropriate width and height, and then dynamically adding the width and height attributes before returning the markup to the browser would be the best choice.
Jon cram
source share