jQuery 我可以打开一个新窗口并用字符串变量填充它吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17493027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:12:23  来源:igfitidea点击:

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

javascriptjqueryhtmldom

提问by arcadeRob

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 use the string the populate the new window. i.e. have the browser take the string and convert it into HTML on the fly. Is this possible?

我正在与看起来很简单的事情进行一场斗争。我有一个 [javascript] 字符串,其中包含 DOM 元素,并想打开一个新窗口 (window.open()?) 并使用该字符串填充新窗口。即让浏览器获取字符串并即时将其转换为 HTML。这可能吗?

回答by Reinstate Monica Cellio

Yes it's possible...

是的,有可能...

var wnd = window.open("about:blank", "", "_blank");
wnd.document.write(html);

That should do the trick.

这应该够了吧。

回答by Kevin Jantzer

HTML

HTML

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

Archer 的回答很好,但如果您愿意,您可以在一个班轮中做到这一点:

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


Opening XML?

打开 XML?

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

With XML, make sure you string begins with <?xml version="1.0" encoding="UTF-8"?>and has a root element. If it doesn't, you can easily add it:

对于 XML,请确保字符串以<?xml version="1.0" encoding="UTF-8"?>根元素开头并具有根元素。如果没有,您可以轻松添加它:

window.open('data:text/xml;charset=utf-8,<?xml version="1.0" encoding="UTF-8"?><RootTag>'+xml+'</RootTag>', "", "_blank")

回答by livin tamil

Archer's answer is the best way. But you need to close the document to run the scripts inside the "htmlString".

Archer的回答是最好的方法。但是您需要关闭文档才能运行“htmlString”中的脚本。

 var wnd = window.open("about:blank", "");
        wnd.document.write(htmlString);
        wnd.document.close();

回答by Mike 'Pomax' Kamermans

Note that while window.openwas a good solution in 2013, at this point in time that is no longer the case, and window.openis not the right answer here anymore; it has become blocked-by-default by almost every browser due to years of abuse by ads, and is frowned upon as a legacy mechanism that completely bypasses the browser history when it doeswork.

请注意,虽然window.open在 2013 年是一个很好的解决方案,但此时已不再如此,并且不再window.open是这里的正确答案;它已成为阻塞按违约几乎所有的浏览器由于多年虐待的广告,并在一个传统的机制,完全绕过该浏览器历史记录时,皱起了眉头的工作。

Instead, build a link anchor element, assign its content as a data-uri, give it a target="_blank"so that it'll open in a new tab, and then trigger a click()on it so that it opens that content as a normal webpage with a normal entry in the browser's history:

相反,构建一个链接锚元素,将其内容分配为数据 uri,给它 atarget="_blank"以便它在新选项卡中打开,然后触发click()它,以便它将该内容作为普通网页打开浏览器历史记录中的条目:

function openAsPageInNewTab(pageContent) {
  let encoded = encodeURIComponent(pageContent); 
  let a = document.createElement(`a`);
  a.target = `_blank`;
  a.href = `data:text/html;charset=utf-8,${encoded}`;
  a.style.display = `none`;
  document.body.appendChild(a); // We need to do this,
  a.click();                    // so that we can do this,
  document.body.removeChild(a); // after which we do this.
}

You might of course still get a popup warning, because it should, but at least you're now doing things in a way that respects users and browsers, unlike the legacy window.openapproach.

您当然可能仍然会收到弹出警告,因为它应该这样做,但至少您现在正在以一种尊重用户和浏览器的方式做事,这与传统window.open方法不同。

回答by Ilya Kyrylov

If you need in new tab you can use this.

如果您需要在新选项卡中,您可以使用它。

const win = window.open('about:blank', '_blank');
win.document.write('<h1>test</h1>');
win.focus();