最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Open new window with css and pictures - Stack Overflow

programmeradmin1浏览0评论

I am doing a simple print option that when click I call a print function. The function copies over the relevant (not all of it) html.

function print() {

var printWindow = window.open("", "Print", "status=no, toolbar=no, scrollbars=yes", "false" );
var toInsert = $("div.book").html();
$(printWindow.document.body).html(toInsert);

}

The problem I have is that this new window doesn't seem to be able to reference my css stylesheet or my pictures that are within the folder. Any ideas? Just focusing on the css issue, would it be possible to insert a <link ... /> into the head of the new window?

Thanks!

I am doing a simple print option that when click I call a print function. The function copies over the relevant (not all of it) html.

function print() {

var printWindow = window.open("", "Print", "status=no, toolbar=no, scrollbars=yes", "false" );
var toInsert = $("div.book").html();
$(printWindow.document.body).html(toInsert);

}

The problem I have is that this new window doesn't seem to be able to reference my css stylesheet or my pictures that are within the folder. Any ideas? Just focusing on the css issue, would it be possible to insert a <link ... /> into the head of the new window?

Thanks!

Share Improve this question edited Jul 27, 2012 at 16:06 zanegray asked Jul 26, 2012 at 22:56 zanegrayzanegray 7687 silver badges13 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3
function Print() {
    var printWindow = window.open("", "Print", "status=no, toolbar=no, scrollbars=yes", "false" );
    $("link, style, script").each(function() {
       $(printWindow.document.head).append($(this).clone())
    });
    var toInsert = $("div.book").html();
    $(printWindow.document.body).append(toInsert);​
}

DEMO

It's a totally new window. It has to have its own CSS etc.

When you write a document into it, you have to write in the <link> tags, <script> tags, and everything else like that.

To dynamically insert a link to an existing CSS stylesheet into the head of the new window this worked well for me:

var cssNode = document.createElement('link');
cssNode.type = 'text/css';
cssNode.rel = 'stylesheet';
cssNode.href = 'http://www.somedomain./styles/FireFox.css';
cssNode.media = 'screen';
cssNode.title = 'dynamicLoadedSheet';
printWindow.document.getElementsByTagName("head")[0].appendChild(cssNode);

Source: Totally Pwn CSS with Javascript - Has some other interesting tricks around direct manipulation of a stylesheet

发布评论

评论列表(0)

  1. 暂无评论