4

The following works in Firefox 4, but not Chrome 10:

<svg:svg version="1.1">
    <svg:use xlink:href="some_file.svg#layer1"/>
</svg:svg>

This is a known bug in Chrome/WebKit, so there's nothing I can do about that except try to find a way to work around it. I thought about using an XMLHttpRequest to grab the external file and insert it into the svg element. Will that cause any problems? Are there better ways to do it?

Mike Baranczak
  • 8,291
  • 8
  • 47
  • 71

4 Answers4

4

After you fetch the SVG document via XHR you will have a separate XML document in the xhr.responseXML property. Since you cannot legally move nodes from one document to another, you'll need to import the portion you want from one document into your target document before you can use it as part of that document.

The simplest way to do this is to use document.importNode():

var clone = document.importNode(nodeFromAnotherDoc,true);
// Now you can insert "clone" into your document

However, this does not work for IE9. To work around that bug, you can alternatively use this function to recursively recreate a node hierarchy in the document of choice:

function cloneToDoc(node,doc){
  if (!doc) doc=document;
  var clone = doc.createElementNS(node.namespaceURI,node.nodeName);
  for (var i=0,len=node.attributes.length;i<len;++i){
    var a = node.attributes[i];
    clone.setAttributeNS(a.namespaceURI,a.nodeName,a.nodeValue);
  }
  for (var i=0,len=node.childNodes.length;i<len;++i){
    var c = node.childNodes[i];
    clone.insertBefore(
      c.nodeType==1 ? cloneToDoc(c,doc) : doc.createTextNode(c.nodeValue),
      null
    );
  }
  return clone;
}

You can see an example of using XHR to fetch an SVG document and both techniques of importing the node on my website: http://phrogz.net/SVG/fetch_fragment.svg

Community
  • 1
  • 1
Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • Yeah, I found the importNode() trick on my own. But then I found out that Android's version of WebKit doesn't support SVG at all, so I decided to forget about SVG altogether and just use regular bitmap images for this project. – Mike Baranczak May 20 '11 at 18:52
  • (Edit: Link added) This is excellent. Thanks, Phrogz, for your dedication to SVG. Your [labs](http://phrogz.net/svg/) helped me from time to time while building [my project](http://www.progettystudio.com). And, wow, I just learned [something new](http://phrogz.net/svg/textRendering.svg) – fregante Jul 16 '11 at 16:57
1

I've written a simple and lighweight polyfill for this: https://github.com/Keyamoon/svgxuse

It detects whether or not it needs to send an HTTP request. If the browser doesn't support external references by default, it sends a GET request to fetch and cache the SVG.

I hope this helps.

Keyamoon
  • 1,785
  • 17
  • 16
0

In case someone stumbles accross this page. Here is a simpler way to use an HTTP request object to fetch the svg file:

window
    .fetch('/assets/ciphers.svg')
    .then(
        function (response) {
            return response.text();
        }
    ).then(
        function (body) {
            var div = document.createElement('div');
            div.innerHTML = body;
            while (div.children.length > 0) {
                document.head.appendChild(div.children[0]);
            }
        }
    );

The trick is in the following line (either you use window.fetch or xmlHttpRequest or whatever):

            var div = document.createElement('div');
            div.innerHTML = body;
            while (div.children.length > 0) {
                document.head.appendChild(div.children[0]);
            }
Lars Juel Jensen
  • 1,643
  • 1
  • 22
  • 31
0

I do a lot of AJAX requests for SVG markup, where I insert the markup into the DOM. You can't just insert it as a fragment, as far as I know, you have to recursively walk the retrieved XML doc, and create the individual SVG elements.

So, you might be better off combining the files on the server, before you send them to the browser.

Josh Pearce
  • 3,399
  • 1
  • 23
  • 24
  • See my answer; other than as a workaround for IE9, there's a much easier way to import nodes into the target document: [`importNode`](https://developer.mozilla.org/en/DOM/document.importNode). – Phrogz May 20 '11 at 17:44