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

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="https://stackoverflow.com/questions/9399354/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/

Leave a Comment