Importing external SVG (with WebKit)

前端 未结 4 1163
说谎
说谎 2020-12-16 07:16

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


    
         


        
4条回答
  •  失恋的感觉
    2020-12-16 07:45

    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]);
                }
    

提交回复
热议问题