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

标签 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 文档代表窗口的完整内容(无 <html></html> 标签)?

最佳答案

在写入文档之前,您需要删除一些元素。我会执行以下操作:

//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 标签的父节点,以及任何子节点/孙节点/等等...

顺便说一句,我会避免使用关键字“new”来命名变量。

关于javascript - 在 javascript 中,如何用 XML 替换窗口的全部内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29133075/

相关文章:

xml - 如何解析 Google Refine 中的 XML 以提取数据?

java - 如何在 Xpath 中获取节点值 - Java

Javascript/jQuery 单击元素中的元素

javascript - 仅在禁用 javascript 时执行操作

javascript - 在悬停图像上播放声音,悬停在图像上时停止

JQuery:无法正确显示表格

javascript - 在调用 javascript 函数后面的代码中添加 onclick 属性,并使用引用 ClientID 的字符串参数

javascript - 在 JavaScript 中创建全局唯一 ID

html - 如果没有表格或预定义的宽度,这种布局是否可行?

java - JSP 将 XML 数据发送到服务器