[SOLVED] Javascript Printer-Friendly Page "Creator"

This is a discussion on "[SOLVED] Javascript Printer-Friendly Page "Creator"" within the JavaScript Forum section. This forum, and the thread "[SOLVED] Javascript Printer-Friendly Page "Creator" are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Closed Thread
 
LinkBack Thread Tools
  #1 (permalink)  
Old Oct 8th, 2007, 20:06
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Javascript Printer-Friendly Page "Creator"

Hello again,

I am trying to work out a small problem I am having. What I am trying to do is to create a link that says 'Print this page', or something like that. When clicked, it will bring the user to print.htm, where a javascript code will output everything that is inside a certain tag, such as
HTML: Select all
<div id="content"></div>
And the link would be
HTML: Select all
<a href="print.htm" target="_blank">Print this page</a>
I have the code:
Code: Select all
 <script language="javascript">
     document.write(window.opener.document.getElementById("content").innerHTML);
     window.print();
</script>
Now the only thing is to not take the inner.HTML of an element with an id, but instead to take everything out of the body. I was thinking this:
Code: Select all
<script language="javascript">
document.write(window.opener.document.getElementsByTagName("body").innerHTML);
</script>
I used document.getElementsByTagName("body") instead of document.getElementById("content"), but it does not seem to work. Print.htm is showing me "undefined". (What is going on here??)

Any help would be appreciated.

Thanks,
SWagner

PS: My server does not allow/support asp, php, cgi, cfm, etc... so using those (although they're much better to use!), is out of the question.
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)

Last edited by Stuart; Oct 8th, 2007 at 20:26.

  #2 (permalink)  
Old Oct 8th, 2007, 20:23
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Javascript Printer-Friendly Page "Creator"

The following may work:
Code: Select all
<script type="text/javascript">
var x=document.getElementById("content").innerHTML;
myWindow=window.open('','MyName','width=200,height=100')
myWindow.document.write(x)
myWindow.print();

</script>
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
  #3 (permalink)  
Old Oct 8th, 2007, 20:25
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Javascript Printer-Friendly Page "Creator"

Tested in FF with the following code:

HTML: Select all
 <html>
<head>
<script type="text/javascript">
function printIT() {
var x=document.getElementById("content").innerHTML;
myWindow=window.open('','MyName','width=200,height=100')
myWindow.document.write(x)
myWindow.print();
}
</script>
</head>

<body>
<a href="#" onClick="printIT();">click</a>
<div id="content">Hello world</div>
</body>
</html>
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
  #4 (permalink)  
Old Oct 8th, 2007, 20:28
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Printer-Friendly Page "Creator"

Thanks for replying, but I am trying to output the entire body.
SWagner
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
  #5 (permalink)  
Old Oct 8th, 2007, 20:30
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Javascript Printer-Friendly Page "Creator"

Oh sorry! Try making your body tag like this:
<body id="content">
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
  #6 (permalink)  
Old Oct 8th, 2007, 20:33
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Printer-Friendly Page "Creator"

I had that idea too, but I have over 50 web pages that I would have to do this with. A little tedious, don't you think?
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
  #7 (permalink)  
Old Oct 8th, 2007, 20:35
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Printer-Friendly Page "Creator"

Are you sure that
Code: Select all
<script language="javascript">
document.write(window.opener.document.getElementsByTagName("body").innerHTML);
</script>
wouldn't work somehow?
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
  #8 (permalink)  
Old Oct 8th, 2007, 20:36
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Javascript Printer-Friendly Page "Creator"

Quote:
Originally Posted by swagner View Post
I had that idea too, but I have over 50 web pages that I would have to do this with. A little tedious, don't you think?
I think I'm missing something.. as either way you will have to edit those 50 pages if they are static html.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
  #9 (permalink)  
Old Oct 8th, 2007, 20:43
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Printer-Friendly Page "Creator"

I am still curious. Javascript has always worked for me. I think that I am just using the wrong code.
Code: Select all
document.getElementsByTagName("body").innerHTML
seems ok, but for some reason, it crashes and displays "undefined" on print.htm. Is it possible that the code is supposed to be getElementByTagName instead of Elements? Maybe it is just some little thing like that...
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
  #10 (permalink)  
Old Oct 8th, 2007, 20:55
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Javascript Printer-Friendly Page "Creator"

try the following:
Code: Select all
<html>
<head>
<script type="text/javascript">
function printIT() {
var spanArray = document.getElementsByTagName('body');
myWindow=window.open('','MyName','width=200,height=100')
myWindow.document.write(spanArray[0].innerHTML)

}
</script>
</head>

<body>
<a href="#" onClick="printIT();">click</a>
<div id="content">Hello world</div>
</body>
</html>
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
  #11 (permalink)  
Old Oct 8th, 2007, 21:02
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Printer-Friendly Page "Creator"

Thanks, it's working great!
Regards,
SWagner
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
  #12 (permalink)  
Old Oct 8th, 2007, 21:03
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Javascript Printer-Friendly Page "Creator"

You're welcome, I wasted some ink on this .
Glad you got it working.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
  #13 (permalink)  
Old Oct 8th, 2007, 21:10
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Printer-Friendly Page "Creator"

One more thing. I have a
Code: Select all
document.write('<img src="file.jpg" />')
on my page (index.htm). Now it is being produced twice on print.htm since the original code (see above) is written, as well as the image written by the code on my page (index.htm). Any solution?
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)

Last edited by Stuart; Oct 8th, 2007 at 21:14.
  #14 (permalink)  
Old Oct 8th, 2007, 21:14
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Javascript Printer-Friendly Page "Creator"

Could you show the source of the entire page?
I'm not too certain of what is happening.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
  #15 (permalink)  
Old Oct 8th, 2007, 21:29
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Printer-Friendly Page "Creator"

Yes, here is the source:
HTML: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>My Page</title>
   
<style type="text/css">
<!--
.style1 {
    font-size: 20px;
    font-family: Georgia, "Times New Roman", Times, serif;
    font-weight: bold;
}
.seperator {color: #acacac;}
.more {font-family: Georgia, "Times New Roman", Times, serif; font-size: 14px; color: #151078; }
-->
</style>
</head>

<body>

<hr />

<div id="sectiontitle"><script type="text/javascript">//<![CDATA[
document.write('<img src="http://www.webforumz.com/images/spacer.gif" id="mainImg" width="740" height="193" alt="" border="0" />');
//]]></script></div>

<hr />

<!-- wrap -->
<div id="wrap" class="threecolumn">

<!-- content -->
<div id="content" class="threecolumn"><br />

<div class="threecolumn">
<p class="style1">
About Us
</p>
<p align="justify">
Text
</p>
<p align="justify">
Text
</p>
<p>
<a href="about.htm" title="">Read more</a><span class="more"> &raquo;</span><br />
<br />
<img src="http://www.webforumz.com/images/spacerGRAY.gif" height="3" width="740" alt="" />
</p>
</div> 
<!-- end -->

</div>
<!-- end content -->

</div>
<!-- end wrap -->
</body>
</html>
The image now appears twice since it is already on this page, written by Javascript. but the Javascript code is also "printed", so on print.htm, the image appears twice.

Thanks
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)

Last edited by Stuart; Oct 10th, 2007 at 12:01.
  #16 (permalink)  
Old Oct 8th, 2007, 22:51
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Printer-Friendly Page "Creator"

I don't like to do this, but ok. Here is the link.
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
  #17 (permalink)  
Old Oct 8th, 2007, 22:57
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Javascript Printer-Friendly Page "Creator"

Okay, is that the page the error is on?
and can you explain the error further?
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
  #18 (permalink)  
Old Oct 8th, 2007, 23:05
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Printer-Friendly Page "Creator"

Yes. The error is in the middle of the page:
HTML: Select all
<div id="sectiontitle"><script type="text/javascript">//<![CDATA[
document.write('<img src="images/spacer.gif" id="mainImg" width="740" height="193" alt="" border="0" />');
//]]></script><noscript><img src="images/banner/noscript.jpg" width="740" height="193" alt="" border="0" /></noscript></div>
This code generates an image. The <noscript> part is not the error! The error is that the image the above code generates is copied into print.htm, the way it is supposed to. Ok so far? Now, the above code is also copied, meaning print.htm ends with two copies of this code: one finished copy (image), one "raw" copy (code). Do you see what I am getting at? There are two copies of this code being copied into print.htm. I need to eliminate one of them.
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
  #19 (permalink)  
Old Oct 8th, 2007, 23:40
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Printer-Friendly Page "Creator"

If you still have problems understanding this, just ask. I know it's a little confusing...
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
  #20 (permalink)  
Old Oct 9th, 2007, 00:06
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Javascript Printer-Friendly Page "Creator"

I personally do not know of a solution I am afraid, maybe a "mentor" can help.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Closed Thread

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] PHP Printer-Friendly Page Stuart PHP Forum 35 Oct 17th, 2007 23:47
[SOLVED] Show "Image" Depends On User "Status"? Monie Classic ASP 6 Oct 16th, 2007 01:22
? IS "meta name="robots" content="?" necessary in pages ? Love2Java Starting Out 6 Aug 8th, 2007 13:48
window.opener.document["nameForm"].getElementById("someid").value; doesnt work drpompeii JavaScript Forum 0 Feb 17th, 2007 23:09
printer-friendly web page marisol Web Page Design 4 Sep 28th, 2006 21:12


All times are GMT. The time now is 10:39.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved