Forum Moderators: open

Message Too Old, No Replies

print preview

print preview

         

sunnny

5:39 pm on Aug 19, 2005 (gmt 0)

10+ Year Member



Hi,

I am trying to make a print preview of a webpage using javascript. I have 2 css style sheets, one for print and one for screen. What I want to do is to show a preview of the printable version when the user clicks on a "printable version" link. I want to open a new window and have the preview of the printable version in it, but I don't know how to do that. I know that someone else was trying to do the same thing and was able to do it, but somehow when I try the same code it doesn't work for me. Here is the link to the post in which someone did the same thing:
[webmasterworld.com...] . I was wondering if someone can help me with this.

Thanks,
Sunny

[edited by: jatar_k at 11:45 pm (utc) on Aug. 29, 2005]
[edit reason] fixed link [/edit]

Rambo Tribble

2:26 am on Aug 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your link is for the login page. All you should need to do is apply a style sheet to the document in the window that is the same as your print stylesheet, but with @media screen rather than @media print (or perhaps, @media screen, print). One way to do that might be to use appendChild, etc. to add a new external style sheet to the document displayed in the window.

sunnny

6:50 pm on Aug 20, 2005 (gmt 0)

10+ Year Member



thanks, for the reply. But i want to make it dynamic and don't want to create a seperate printer friendly page. Oh and how do you post a link to a post in the forum.

Sunny

Rambo Tribble

5:19 am on Aug 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Links to personal pages are not allowed under the terms of service.

Using a script to append a style sheet to the document in the window should allow you to reuse the same page while changing the layout.

sunnny

4:08 pm on Aug 21, 2005 (gmt 0)

10+ Year Member



Thanks, for your reply. Could you post a piece of code, I tried something like this but it didn't work, maybe I am doing something wrong.

Thanks,
Sunny

Rambo Tribble

2:37 am on Aug 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note that the setTimeout is necessary to give the window time to build before applying the new style. You may need to increase the timeout value for a larger file. Unfortunately, window.onload does not seem to work in this particular case.

Contents of tst_378_sty.css:


p{font-size:1em;}

Contents of main file:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
p{font:3em Verdana,sans-serif;}
</style>
<script type="text/javascript">
function winBuild(){
newWin=window.open('tst_378.htm','','height=300,width=400');
tOut=setTimeout(appSty,2);
}
function appSty(){
var win_style=newWin.document.createElement('link');
win_style.href="tst_378_sty.css";
win_style.rel="stylesheet";
win_style.type="text/css";
newWin.document.getElementsByTagName('head')[0].appendChild(win_style);
newWin.focus();
}
</script>
</head>
<body>
<p onclick="winBuild();">
TEXT
</p>
</body>
</html>

Rambo Tribble

4:05 am on Aug 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I thought of a way to use window.opener to employ window.onload, which is a bit less of a kludge than setTimeout. Replace the main file with this:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
p{font:3em Verdana,sans-serif;}
</style>
<script type="text/javascript">
if(self.opener){
window.onload=opener.appSty;
}
function winBuild(){
newWin=window.open('tst_378.htm','','height=300,width=400,top=200,left=300');
}
function appSty(){
var win_style=newWin.document.createElement('link');
win_style.href="tst_378_sty.css";
win_style.rel="stylesheet";
win_style.type="text/css";
newWin.document.getElementsByTagName('head')[0].appendChild(win_style);
newWin.focus();
}
</script>
</head>
<body>
<p onclick="winBuild();">
TEXT
</p>
</body>
</html>

Rambo Tribble

3:09 pm on Aug 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh, and tst_378.htm is the name I used for the main file, if it's not obvious.

sunnny

7:08 pm on Aug 22, 2005 (gmt 0)

10+ Year Member



Thanks alot for your help! I will try this code and let you know what happens.

cheers,
Sunny

sunnny

1:51 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



Hi,

Everything works great, one small problem though, when the style is applied to the new window there is a delay before the style sheet is switched so I see the original style sheet for a while before it switches to the next one. Any ideas on why this is happening?

Thanks,
Sunny

Rambo Tribble

4:15 pm on Aug 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's probably due to the new style sheet having to be retrieved from the server. A potential solution would be to link to the style sheet before the other styles in the original page, so the subsequent styles override its rules. Then, when it's appended, it should again override the other styles.

sunnny

6:01 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



yes, but the href is still calling the file from the server, so even if we load it, it still gets it from the server because of the path. Is there a way that we can preload it like we can do with images?

Thanks,
Sunny

Rambo Tribble

10:34 pm on Aug 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The file should be cached locally and, despite the href, be pulled from cache. This is one of the primary reasons to use an external style file in the first place.

sunnny

8:17 pm on Aug 29, 2005 (gmt 0)

10+ Year Member



I tried putting it before the other style sheets like you said, but it still takes time to load the print style sheet. Also, even if I refresh the page it takes time to load the print stylesheet. Any ideas?

Thanks for all your help!
Sunny

Rambo Tribble

10:35 pm on Aug 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was afraid that might happen if the page is large. It might be better to live with the problem than implement the solution. That said, I think you could use an initial style sheet with:

body{display:none;}

in conjunction with a script like:

window.onload=function(){
// code to modify styles if this is a child window
document.body.style.display='block';
}

Rambo Tribble

2:37 am on Aug 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Upon further reflection it occurred that you just might get away with a line in the if(self.opener) block, right after the conditional, that set the display to none at that time, before setting the window.onload function.

What that might successfully accomplish is not having to use display:none as the default on the original page, thereby not delaying the initial page display.

sunnny

7:36 pm on Aug 30, 2005 (gmt 0)

10+ Year Member



okay i will give it a try, so i should put css code inside 'code to change the styles'

Rambo Tribble

3:51 am on Aug 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this. It may not work on all browsers, you'll need to test it.

tst_378.htm:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
p{font:3em Verdana,sans-serif;}
</style>
<script type="text/javascript">
if(self.opener){
window.onload=appSty;
}
function winBuild(){
newWin=window.open('tst_378.htm','','height=300,width=400,top=200,left=300');
}
function appSty(){
var win_style=document.createElement('link');
win_style.href="tst_378_sty.css";
win_style.rel="stylesheet";
win_style.type="text/css";
document.getElementsByTagName('head')[0].appendChild(win_style);
self.focus();
}
</script>
</head>
<body>
<script type="text/javascript">
if(self.opener){
var bod_disp=document.createElement('style')
var sty_txt=document.createTextNode('body{display:none;}');
bod_disp.appendChild(sty_txt);
document.getElementsByTagName('head')[0].appendChild(bod_disp);
}
</script>
<p onclick="winBuild();">
TEXT
</p>
</body>
</html>

tst_378_sty.css:


p{font-size:1em;}
body{display:block;}

Rambo Tribble

4:18 am on Aug 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Come to think of it, you might be able to get away with just this. Again you'll need to test it.

tst_378_b.htm


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
p{font:3em Verdana,sans-serif;}
</style>
<script type="text/javascript">
function winBuild(){
newWin=window.open('tst_378_b.htm','','height=300,width=400,top=200,left=300');
}
</script>
</head>
<body>
<script type="text/javascript">
if(self.opener){
var win_style=document.createElement('link');
win_style.href="tst_378_sty_b.css";
win_style.rel="stylesheet";
win_style.type="text/css";
document.getElementsByTagName('head')[0].appendChild(win_style);
self.focus();
}
</script>
<p onclick="winBuild();">
TEXT
</p>
</body>
</html>

tst_378_sty_b.css:


p{font-size:1em;}