How to open a new window and insert html into it using jQuery?

Try this:

var x=window.open();
x.document.open();
x.document.write('content');
x.document.close();

I find it works in Chrome and IE.


Building upon @Emre's answer.

With javascript, you can chain, so I just modified the code to:

var x=window.open();
x.document.open().write('content');
x.close();

Also, to force it to a new window (not a new tab), give the first line dimensions. But it has to be the third argument. So change the first line to:

var x=window.open('','','width=600, height=600');

try:

var x = window.open('', '', 'location=no,toolbar=0');
x.document.body.innerHTML = 'content';

Here's an example to open a new window with content using jQuery

<script>
function nWin() {
  var w = window.open();
  var html = $("#toNewWindow").html();

    $(w.document.body).html(html);
}

$(function() {
    $("a#print").click(nWin);
});​
</script>

<div id="toNewWindow">
    <p>Your content here</p>
</div>

<a href="javascript:;" id="print">Open</a>​

EDIT: For those who say that this code doesn't work, here's a jsfiddle to try it http://jsfiddle.net/8dXvt/