1

I have about 100 svg files, and for all I want to change the same thing. This is the original:

<svg version="1.1" id="Laag_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     width="460px" height="104px" viewBox="0 0 460 104" enable-background="new 0 0 460 104" xml:space="preserve">

And I want to change it like this:

<svg version="1.1" id="Laag_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     preserveAspectRatio="xMinYMin meet" viewBox="0 0 460 104" enable-background="new 0 0 460 104" xml:space="preserve">

My html code is:

<div id="main"><object width="100%" data="http://euclidthegame.com/mathexchange/7.svg" type="image/svg+xml"></object></div>

So I want to set height and width to default, and I want to add preserveAspectRatio="xMinYMin meet" . Of course I could just edit all the files, but I would like to do this externally using, javascript,jquery or css. I'm not sure how I can change the attributes in an external svg.

Here is what I tried: http://jsfiddle.net/kasper90/vV7Y8/

Kasper
  • 12,594
  • 12
  • 41
  • 63
  • What environment are you working in (Windows, Linux) ? If none of these solutions are working for you, I can provide a script that will accomplish the change in the actual files without you having to go through each one manually. – tylerauerbeck Jun 22 '13 at 18:39
  • Have you looked into using SVG Cleaner (http://libregraphicsworld.org/blog/entry/svg-cleaner-0.5-leaner-faster-runs-on-mac), SVG Scour (https://launchpad.net/scour) or SVGo (https://github.com/svg/svgo)? – Erik Dahlström Jun 24 '13 at 08:51

1 Answers1

1

Possible solution: to avoid editing 100 svg files, convert it to inline <svg>, the code is found in my answer to this SO question.

And tweak the code to insert preserveAspectRatio:-

$aspectRatio = "xMinYMin meet";
$svg = $svg.attr('preserveAspectRatio', $aspectRatio);

Speed: as the <svg> images are already loaded, a $.get() will result in HTTP Error 304 - Not modified and contents would be read from browser's cache.

Community
  • 1
  • 1
Alvin K.
  • 4,329
  • 20
  • 25