0

I am creating an XML string using JQuery. I would like to open a new window to display this string to the user in order he saves it as an XML file.

I do not have a server side and I would like this Javascript script to be compatible with either Firefox and Internet Explorer browsers.

I found a lot of stuff but nothing works really well.

uriContent="data:application/xml," + encodeURIComponent(xmlContent);
var newWindow=window.open(uriContent,'_blank','toolbar=0,location=0,directories=0,status=0, scrollbars=1, resizable=1, copyhistory=1, menuBar=1, width=640,height=480, left=50, top=50');

or for IE:

var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(xmlContent);
var newWindow = window.open('','_blank','toolbar=0, location=0, directories=0, status=0, scrollbars=1, resizable=1, copyhistory=1, menuBar=1, width=640, height=480, left=50, top=50', true);
 newWindow.document.writeln(xmlDoc.documentElement.xml);
 newWindow.document.close();

The first solution works almost fine for Firefox but not for IE:

a data: url in FF

The second source code opens a window with the XML content but IE does not recognize it as XML... So the user has to display the source code by himself. This is not very convenient.

empty IE window

Does anyone have a solution?

Thanks!

4
  • 1
    Should the user see the XML source (the tags) or a rendering (via CSS or XSLT) of the XML? If the first, why not simply putting it in a <pre> element? Commented Jun 13, 2012 at 14:30
  • Browsers are best at displaying HTML, not XML. That said, your second approach is the correct one. Commented Jun 13, 2012 at 14:35
  • The user should see XML tags. Indeed I didn't try with <pre> element. Unfortunately, the second approach does not work. Commented Jun 13, 2012 at 14:38
  • @Boldewyn - Whoops! Didn't read your comment before posting my answer. Commented Jun 13, 2012 at 14:41

1 Answer 1

2

What if you treat the XML as content?

var newWindow = window.open('','_blank','toolbar=0, location=0, directories=0, status=0, scrollbars=1, resizable=1, copyhistory=1, menuBar=1, width=640, height=480, left=50, top=50', true);
var preEl = newWindow.document.createElement("pre");
var codeEl = newWindow.document.createElement("code");
codeEl.appendChild(newWindow.document.createTextNode(xmlContent));
preEl.appendChild(codeEl);
newWindow.document.body.appendChild(preEl);

... then you can use something like Google Code Prettify or SyntaxHighlighter to add whatever highlighting you need.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that's exactly what I'd do. +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.