开发者

How does one open a documentFragment in a new tab

开发者 https://www.devze.com 2023-02-04 02:24 出处:网络
What options are there for opening a document fragment in a new tab or window. Which are most browser compatible and which are most user friendly?

What options are there for opening a document fragment in a new tab or window. Which are most browser compatible and which are most user friendly?

var frag = document.createDocumentFragment();
var div = $("<div>").addClass("Printable");
div[0].innerHTML = doc.body.innerHTML;
frag.appendChild(div[0]);
openIt(frag); // How to implement openIt

The fragment contains a reformatted subset of the page that is ready to be printed.

Preferably I would like it to open naturally throug开发者_如何学Pythonh some kind <link> or <a> and look like any other user friendly html link, rather then quietly opening up an annoying pop-up through window.open.


You can't reliably do this without window.open() and even then you need to create the fragment on that document, rather than the current (as some browsers don't allow cross-appending elements. It would look something like this:

var win = window.open("","myWindow","...options...");
var frag = win.document.createDocumentFragment();
var div = win.document.createElement("div");
div.className = "Printable";
div.innerHTML = doc.body.innerHTML;
frag.appendChild(div);

You can test out a demo here.


Try this. window.open('data:text/html;charset=utf-8,text%20to%20show');

edited:

That was the basic ... This should suit your needs:

var text = "<div class='Printable'>" + $("body").html() + "</div>";
window.open('data:text/html;charset=utf-8,'+text);
0

精彩评论

暂无评论...
验证码 换一张
取 消