Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printing 1

Status
Not open for further replies.

Dinobot2

Technical User
Aug 9, 2005
75
US
I'm looking for the code to print a page in javascript and
all I found was windows.print()

it prints the page but what I want it to do is to print a popup window or a document.

is there some method I can use like windows.print(document name) to print one document or windows.print(popup window)to print a popup window?

Really need the help
 
If you open a popup with window.open(), you can try something like this:
Code:
var popupwin = window.open('popup.htm', 'popupwin', 'window parameters here');
popupwin.print();

Lee
 
Here is my code. it didn't work even after I assigned a variable.


<html>

var popupwin=window.open('ad1.htm');
<form>


<input type="button" value=" Print this page Now !"
onclick="popupwin.print()">

</form>


------------------------------------------------------------------------------<br><br>
 
If you use my example as I wrote it, it will work for you, though you might have popups blocked if you're viewing the page locally. I copied and pasted the code example I provided (changing the URL to a page I already had) and tried it using IIS. The popup window opened and then the print dialog opened.

Lee
 
Tried it again and still didn't work. are you using ASP to run it? meaning that you have to load IIS on your PC then place it into inetpub for it to work?
 
Show your code. You said it doesn't work, but didn't give any details about what happens. That's similar to telling a doctor "I'm sick" when (s)he asks what's wrong with you. You have to provide useful information to get help. I tried it and know that the code works as I wrote it, so there must be something else causing the problem if you used exactly what I wrote.

I meant I was running the page in a web server rather than locally, not that you need IIS. Client-side Javascript doesn't need ASP or any other scripting language installed.

Lee
 
This is pretty much it. I was going to attach it to a web doucment and have a print button , print the printer-
friendly version of the document.

<html>

<title> Welcome to my site</title>

<header>
<p> if you want to print the printer friendly version of this page click here</P>

<form>

var popupwin=window.open('ad1.htm','popupwin');
<input type="button" value=" Print this page Now !"
onclick="popupwin.print()">

</form>
</header>

<body>

</body>
</html>


I'm very very basic in writing scripts and when I tried it with this, it didn't work
 
I copied and pasted YOUR code, added script tags to the javascript code, and the print dialog appeared when I clicked on the button. Do you have script tags around the code that opens a popup?

Lee
 
Ok. I didn't have the script tags I added it and it autoloaded the window. the print button works.
thanks.
Just one thing. Why is it autoloading the window at startup?

<html>

<title> Welcome to my site</title>

<header>
<p> if you want to print the printer friendly version of this page click here</P>


<script type="text/javascript">

var popupwin=window.open('ad1.htm','popupwin'," ")


</script>


<form>
<input type="button" value=" Print this page Now !" onClick="popupwin.print()">
</form>


</header>

<body>

</body>
</html>
 
Never mind. Figured it out. it works

Thanks.

here's the new code.

<html>

<title> Welcome to my site</title>

<header>
<p> if you want to print the printer friendly version of this page click here</P>


<script type="text/javascript">

function pete(){
var popupwin=window.open('ad1.htm','popupwin'," ");
popupwin.print();
}

</script>


<form>
<input type="button" value=" Print this page Now !" onClick="pete()">
</form>


</header>

<body>

</body>
</html>
 
It's generally better to name variables and functions something descriptive of what they are or do rather than use personal or cryptic names. Off the top of my head, I'd suggest something like openPrinterFriendlyPage().

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top