1

I've been trying to pass some xml contents to a new browser window using the following technique:

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

However, the xml gets placed inside <body></body> tags in the new window.

How can I make my xml doc represent the FULL contents of the window (no <html></html> tag)?

Troy
  • 21,172
  • 20
  • 74
  • 103
  • 2
    [relevant](http://stackoverflow.com/questions/5581592/render-xml-document-obtained-through-ajax-call-to-a-new-window) – Sterling Archer Mar 18 '15 at 21:35
  • 2
    @SterlingArcher Perfect: http://jsfiddle.net/1sh9tyq9/ (don't forget to allow popups) – blex Mar 18 '15 at 21:38
  • @SterlingArcher - Thanks, I had seen that question, however, it only becomes relevant after I figure out how to make the `` tags go away. It's step #2 once I get the answer to this question. ;) – Troy Mar 18 '15 at 21:38
  • @blex - Perfect! Post that as an answer and I'll accept it. – Troy Mar 18 '15 at 21:42
  • @Troy Nah, that's a duplicate question. My JS Fiddle just uses the technique used in Sterling Archer's link. But if someone else wants to, I won't stop them ;) – blex Mar 18 '15 at 21:44
  • 1
    That's Duchess to you, blex. – Sterling Archer Mar 18 '15 at 21:46
  • @blex - Sure enough. I had missed it in that post (the answer in case anyone else needs it: http://stackoverflow.com/questions/5581592/#answer-5582205). Thanks! – Troy Mar 18 '15 at 21:47
  • This doesn't work if you have very large xml strings. – Tym Pollack Jul 28 '15 at 17:43

1 Answers1

-1

Before writing to the document, you need to remove some elements. I would do the following:

//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

This will clear the html tags, which are the parent node of the body tags, and anything that is a child/grandchild/and so on...

As an aside, I would avoid naming your variables with the keyword "new" in them.

rdgd
  • 1,451
  • 2
  • 18
  • 33