在javascript中,如何用XML替换窗口的全部内容?

时间:2015-03-18 21:29:49

标签: javascript html xml

我一直在尝试使用以下技术将一些xml内容传递到新的浏览器窗口:

    var newWindow = window.open('', '_blank');
    newWindow.document.clear();
    newWindow.document.open();
    newWindow.document.write(xmlString);
    newWindow.document.close();

但是,xml会被放置在新窗口中的<body></body>标记内。

如何让我的xml doc代表窗口的完整内容(没有<html></html>标签)?

1 个答案:

答案 0 :(得分:0)

在写入文档之前,您需要删除一些元素。我会做以下事情:

//The following performed on the new document you are creating

var tagsToDestroy = newWindow.document.querySelector('html');
newWindow.document.removeChild(tagsToDestroy);

//Then write your content and do whatever else

这将清除html标签,它是body标签的父节点,以及任何子/孙/等等......

顺便说一句,我会避免使用关键字&#34; new&#34;来命名变量。在他们中。