2015-03-18 51 views
1

我一直在試圖將一些XML內容傳遞給使用以下技術一個新的瀏覽器窗口:在JavaScript中,如何用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>標籤)?

+2

[相關](http://stackoverflow.com/questions/5581592/render-xml-document-obtained-through-ajax-call-to-a-new-window) – 2015-03-18 21:35:52

+2

@SterlingArcher完美:http:// jsfiddle.net/1sh9tyq9/(不要忘記允許彈出窗口) – blex 2015-03-18 21:38:29

+0

@SterlingArcher - 謝謝,我已經看到了這個問題,但是,只有在我找出如何讓''標籤消失後纔會變得相關。一旦我得到這個問題的答案,它就是第二步。 ;) – Troy 2015-03-18 21:38:50

回答

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標籤,這是身體標籤的父節點,任何一個孩子/孫子/等等...

由於另外,我會避免在關鍵字中使用關鍵字「new」命名變量。

相關問題