Forum Moderators: open
print.html
=========================
<html>
<head>
<script type="text/javascript">
function printExternal() {
printWindow = new Object();
printWindow.window.location = "index.html";
printWindow.window.print();
}
</script>
</head>
<body>
<form>
<input type="button" name="print" value="Print" onclick="printExternal();" />
</form>
</body>
</html>
index.html
=========================
<html>
<body>
<h1>Print Test!</h1>
</body>
</html>
Thanks.
printWindow = new Object();
printWindow.window.location = "index.html";
printWindow.window.print();
You've created a new object. A new object contains nothing. It's kinda like a blank canvas for you to use later.
You somehow seem to have coded it as if it's miraculously going to turn into another page?
I personally would approach this from a popup point of view.....
printWindow = window.open( myWindow, "index.html");
printWindow.print();
printWindow.close();
Your print.html has a hidden frame that points to index.html
You need to add a little function to index.html that will print that page;
function CallPrint(){
self.focus()
self.print()
}
So on print.html you simply call the function on index.html, something like; document.getElementById('yourFrame').CallPrint();
I haven't tried it but it should work.
print.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title> New Document </title>
</head>
<body><iframe name="toPrint" src="index.html"></iframe>
<form>
<input type="button" onclick="document.toPrint.printThePage();" value="Print index.html">
</form></body>
</html>
index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title> New Document </title>
<script type="text/javascript">
function printThePage(){
self.focus()
self.print()
}
</script>
</head>
<body><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer tincidunt aliquam metus. Quisque rutrum pretium odio. Aliquam erat volutpat.</p>
</body>
</html>
I have seen this work on a demo page posted somewhere but haven't tested it myself.
It might solve your problem completely and it's a cool thing to know, absolutely.
If you put this link tag in the head of the page, when the user goes to print it will print out the alternate page.
<link rel=alternate media=print href="print.html">
Let us know if this works out... please.
(Save me the trouble because now my curiosity is piqued.)
I tested it in Firefox both under Windows and Linux.
Seriously though, sorry it doesn't work in FX. I don't have it installed so I can't debug it for you but I'm sure it's something very minor.
Unless you have several print buttons for printing different sections of the page, poppyrich's suggestion is probably better anyways (assuming it works).