Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open inline SVG in new window - Javascript

Tags:

javascript

svg

I have a <div> that contains an inline svg. I would like a function that opens that svg in a new tab/window. I only want to use the front-end js without having to save the svg anywhere.

If I use window.open(), it puts the svg inside an <html> tag which I'm trying to avoid.

I'm basically trying to change this answer but then only have the svg code left: https://stackoverflow.com/a/21667339/1083923

//---print button---
    var printSVG = function()
    {
        var popUpAndPrint = function()
        {
            var container = $('#svgDiv');
            var width = parseFloat(container.getAttribute("width"))
            var height = parseFloat(container.getAttribute("height"))
            var printWindow = window.open('', 'PrintMap',
            'width=' + width + ',height=' + height);
            printWindow.document.writeln($(container).html());
            printWindow.document.close();
            printWindow.print();
            printWindow.close();
        };
        setTimeout(popUpAndPrint, 500);
    };
like image 668
BelgoCanadian Avatar asked Sep 13 '17 17:09

BelgoCanadian


People also ask

Can SVG integrate with JavaScript?

Details about each SVG element. Details about each SVG attribute. Details about the SVG DOM API, for interaction with JavaScript. SVG works together with HTML, CSS and JavaScript.

Should SVG be inline?

Because inline SVG is embedded into HTML, there is no necessity for another network request to obtain the SVG file, and therefore inline SVG will load the fastest.

How do I embed an SVG file?

SVG images can be written directly into the HTML document using the <svg> </svg> tag. To do this, open the SVG image in VS code or your preferred IDE, copy the code, and paste it inside the <body> element in your HTML document.

What is inline SVG?

Inline SVG simply refers to SVG markup that is included in the markup for a webpage.


1 Answers

You can stringify your element into a valid SVG document and store it as a Blob, then point your new Window to that Blob using a blob:// URI:

// we need to handle a user gesture to use 'open'
document.getElementById("btn").onclick = (evt) => {
  // grab your svg element
  const svg = document.querySelector("svg");
  // convert to a valid XML source
  const as_text = new XMLSerializer().serializeToString(svg);
  // store in a Blob
  const blob = new Blob([as_text], { type: "image/svg+xml" });
  // create an URI pointing to that blob
  const url = URL.createObjectURL(blob);
  const win = open(url);
  // so the Garbage Collector can collect the blob
  win.onload = (evt) => URL.revokeObjectURL(url);
};

JS-fiddle demo since StackSnippet's sand-boxed iframes don't allow opening new windows.

like image 103
Kaiido Avatar answered Oct 10 '22 07:10

Kaiido