Javascript - 在浏览器中渲染动态生成的XML

时间:2012-06-13 14:25:54

标签: javascript xml browser

我正在使用JQuery创建XML字符串。我想打开一个新窗口向用户显示该字符串,以便将其保存为XML文件。

我没有服务器端,我希望这个Javascript脚本与Firefox和Internet Explorer浏览器兼容。

我找到了很多东西,但没有什么效果很好。

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');

或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();

第一个解决方案适用于Firefox,但不适用于IE:

a data: url in FF

第二个源代码打开一个包含XML内容的窗口,但IE不会将其识别为XML ...因此用户必须自己显示源代码。这不太方便。

empty IE window

有没有人有解决方案?

谢谢!

1 个答案:

答案 0 :(得分:2)

如果将XML视为内容,该怎么办?

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

...然后您可以使用Google Code PrettifySyntaxHighlighter之类的内容添加您需要的任何突出显示。

相关问题