Webforumz's RSS FeedRSS Webforumz RegistrationRegister Contact Webforumz StaffContact

[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.


 Subscribe in a reader

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

Notices




Closed Thread
 
LinkBack Thread Tools
  #1  
Old Oct 8th, 2007, 21: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 21:26.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #2  
Old Oct 8th, 2007, 21:23
alexgeek's Avatar
Moderator
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,814
Blog Entries: 9
Thanks: 2
Thanked 3 Times in 3 Posts
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>
__________________
Web Design and Development Blog

Alex Perry
Technical Administrator.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #3  
Old Oct 8th, 2007, 21:25
alexgeek's Avatar
Moderator
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,814
Blog Entries: 9
Thanks: 2
Thanked 3 Times in 3 Posts
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>
__________________
Web Design and Development Blog

Alex Perry
Technical Administrator.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #4  
Old Oct 8th, 2007, 21: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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #5  
Old Oct 8th, 2007, 21:30
alexgeek's Avatar
Moderator
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,814
Blog Entries: 9
Thanks: 2
Thanked 3 Times in 3 Posts
Re: Javascript Printer-Friendly Page "Creator"

Oh sorry! Try making your body tag like this:
<body id="content">
__________________
Web Design and Development Blog

Alex Perry
Technical Administrator.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #6  
Old Oct 8th, 2007, 21: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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #7  
Old Oct 8th, 2007, 21: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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #8  
Old Oct 8th, 2007, 21:36
alexgeek's Avatar
Moderator
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,814
Blog Entries: 9
Thanks: 2
Thanked 3 Times in 3 Posts
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.
__________________
Web Design and Development Blog

Alex Perry
Technical Administrator.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #9  
Old Oct 8th, 2007, 21: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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #10  
Old Oct 8th, 2007, 21:55
alexgeek's Avatar
Moderator
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,814
Blog Entries: 9
Thanks: 2
Thanked 3 Times in 3 Posts
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>
__________________
Web Design and Development Blog

Alex Perry
Technical Administrator.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #11  
Old Oct 8th, 2007, 22: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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #12  
Old Oct 8th, 2007, 22:03
alexgeek's Avatar
Moderator
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,814
Blog Entries: 9
Thanks: 2
Thanked 3 Times in 3 Posts
Re: Javascript Printer-Friendly Page "Creator"

You're welcome, I wasted some ink on this .
Glad you got it working.
__________________
Web Design and Development Blog

Alex Perry
Technical Administrator.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #13  
Old Oct 8th, 2007, 22: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 22:14.
Digg this Post!