【问题标题】:In javascript, how do I replace the full contents of a window with XML? [duplicate]在 javascript 中,如何用 XML 替换窗口的全部内容? [复制]
【发布时间】:2015-05-21 21:18:10
【问题描述】:

我一直在尝试使用以下技术将一些 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> 标记)?

【问题讨论】:

  • @SterlingArcher Perfect: jsfiddle.net/1sh9tyq9(别忘了允许弹出窗口)
  • @SterlingArcher - 谢谢,我已经看到了这个问题,但是,只有在我弄清楚如何使 <html></html> 标记消失之后,它才变得相关。一旦我得到这个问题的答案,这是第 2 步。 ;)
  • @blex - 完美!将其发布为答案,我会接受。
  • 那是你的公爵夫人,blex。

标签: javascript html xml


【解决方案1】:

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

//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”来命名变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-14
    • 2013-02-27
    • 2023-03-04
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 2014-03-11
    • 2016-12-01
    相关资源
    最近更新 更多