Can I open a new window and populate it with a string variable?

后端 未结 5 1915
别跟我提以往
别跟我提以往 2021-02-12 03:53

I am having a bit of a battle with something that seems simple. I have a [javascript] string that has DOM elements in it and would like to open a new window (window.open()?) and

5条回答
  •  情书的邮戳
    2021-02-12 04:04

    HTML

    Archer's answer is a good one, but you can do this in a one liner if you wish:

    window.open("data:text/html;charset=utf-8,"+html, "", "_blank")
    

    Opening XML?

    window.open("data:text/xml;charset=utf-8,"+xml, "", "_blank")
    

    With XML, make sure you string begins with and has a root element. If it doesn't, you can easily add it:

    window.open('data:text/xml;charset=utf-8,'+xml+'', "", "_blank")
    

提交回复
热议问题