简体   繁体   中英

Putting HTML from the current page into a new window

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
});

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);
  }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM