4

I want to open a new window and carry over some of the HTML in the original page to the new window. What is the simplest way to do this?

Something like:

$("div#foo").click( function(){
       var copyHTML = $("table.bar").html();
       window.open(''); // somehow put copyHTML in the new window
});
Matthew
  • 7,605
  • 7
  • 39
  • 39
  • After further review, it seems like a better option is to manipulate the HTML on the current page rather than try to copy a selection of it over to a new window. – Matthew Sep 17 '09 at 16:48

1 Answers1

3

Try the following:

$("div#foo").click
(
  function()
  { 
    var copyHTML = $("table.bar").html();
    var newWindow = window.open('');
    newWindow.document.body.innerHTML = copyHTML;
  }
);

This will work in some cases, and is the easier than the next approach.

If you get security warnings from your browser, the next approach may be more agreeable. Add a function within the parent page called getContent, like so:

function getContent()
{
  return $("table.bar").html();
}

...and on document.ready in the child window do the following:

$(document).ready
(
  function()
  {
    var parentContent = window.opener.getContent();
    $("body").html(parentContent);
  }
);
David Andres
  • 31,351
  • 7
  • 46
  • 36
  • 1
    Deleted my answer because this one is sooo much more correct. I wasn't even aware you could access things like "opener" – idrumgood Sep 17 '09 at 16:53
  • Even more elegant is http://stackoverflow.com/questions/1225558/jquery-new-window-with-content-from-modal-content-on-opener-page which will avoid security warnings without requiring javascript in the child window – Anson Kao Nov 21 '11 at 04:56