Will Knight
Will Knight

Reputation: 53

Open XML in new window with javascript

I would like to be able to view xml data using any browser's native xml formatting. Similar to opening a local xml file in a browser.

  1. The xml data is stored as a string which javascript has access to.
  2. I do not need anything else on the web page other than the xml data.

    var xmlString = document.getElementById("xmlDivContent" + name).innerText; window.open("data:text/xml;charset=utf-8," + xmlString, "", "_blank");

I've searched around, extensively, for a solution to this problem...I'm not interested in using XSLT or any "home-rolled" formatting function because I just want to take advantage of the browser's built-in xml formatting.

Upvotes: 3

Views: 7273

Answers (2)

Joshua Shaffer
Joshua Shaffer

Reputation: 116

This is possible using the Blob APIs:

let blob = new Blob(['<yourxmlstringhere></yourxmlstringhere>'], {type: 'text/xml'});
let url = URL.createObjectURL(blob);
window.open(url);
URL.revokeObjectURL(url); //Releases the resources

Upvotes: 10

Will Knight
Will Knight

Reputation: 53

This used to be achievable by simply creating a data URL containing the encoded xml information. Most browsers, notably Chrome, do not support this functionality in a ticket described here: Intent to Deprecate and Remove: Top-frame navigations to data URLs

In this post they detail the possible alternatives as:

  • Generate the file on the backend and send it to the user over http/https.

  • Initiate a download instead of displaying the URL.

  • If the contents of the URL is trusted, iframe the URL so that the omnibox displays the site's URL.

I ended up downloading the xml. If someone could come up with a solution for displaying the XML contents in an iframe (using a browser's native xml formatting, that would be a nice improvement)

Upvotes: 0

Related Questions