Frosty555
asked on
Open popup, then call javascript function in the popup
This is a really easy one, but I'm still having trouble >_>
I have one page which displays a list of invoices. Each invoice has a little "print" button beside it.
I also have a print-view type page for each invoice. Inside this page is a javascript function called startPrint() which hides some elements on the page and uses ScriptX to print the page out.
So what I wanted to do was open a popup from my list page, which displayed the print page. Then I wanted to call the print page's "startPrint()" function, and finally close the page, all in one action.
I have this so far for my link:
<a onclick="oW = window.open('', 'print', 'scrollbars=no,width=50,he ight=50'); oW.startPrint(); oW.close()" target="print" href="invoice_print.php?in voiceid=3" >Print</a>
And the popup opens, in the right place, with the right page in it. But the startPrint() function can't be found.
Here's how I declared the startPrint() function in the invoice_print.php page:
<head>
...
<script>
function startPrint()
{
document.all.print.style.v isibility = 'hidden';
ScriptXPrint();
setTimeout('document.all.p rint.style .visibilit y = \'visible\'', 500);
}
</script>
...
</head>
Can anyone help me make this function call work?
I have one page which displays a list of invoices. Each invoice has a little "print" button beside it.
I also have a print-view type page for each invoice. Inside this page is a javascript function called startPrint() which hides some elements on the page and uses ScriptX to print the page out.
So what I wanted to do was open a popup from my list page, which displayed the print page. Then I wanted to call the print page's "startPrint()" function, and finally close the page, all in one action.
I have this so far for my link:
<a onclick="oW = window.open('', 'print', 'scrollbars=no,width=50,he
And the popup opens, in the right place, with the right page in it. But the startPrint() function can't be found.
Here's how I declared the startPrint() function in the invoice_print.php page:
<head>
...
<script>
function startPrint()
{
document.all.print.style.v
ScriptXPrint();
setTimeout('document.all.p
}
</script>
...
</head>
Can anyone help me make this function call work?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Problem with doing it that was is that the window.close() pops up a confirmation box asking me if I'm sure I want to close the window... which defeats the purpose o.O
My hope here was that opening the popup as a child window would allow me to close it later without that confirmation. But to do that, I need to wait until the printing is actually finished, aka when startPrint() returns.
You see my dilemma here? If I just opened the popup window from the list page, and then closed it, it
would open and close right away without the body.onload ever getting called, and there's no obvious way for me to know really when the body.onload has finished executing. That's why I wanted to call startPrint() from the list page.
My hope here was that opening the popup as a child window would allow me to close it later without that confirmation. But to do that, I need to wait until the printing is actually finished, aka when startPrint() returns.
You see my dilemma here? If I just opened the popup window from the list page, and then closed it, it
would open and close right away without the body.onload ever getting called, and there's no obvious way for me to know really when the body.onload has finished executing. That's why I wanted to call startPrint() from the list page.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
How about putting the window.close at the end of the printPage() function?
ASKER
Okay, I figured it out. Ben your comment is what tipped me off.
You see my call to open the popup window:
<a onclick="oW = window.open('', 'print', 'scrollbars=no,width=50,he ight=50'); oW.startPrint(); oW.close()" target="print" href="invoice_print.php?in voiceid=3" >Print</a>
Notice how the window.open() call doesn't actually have the URL in it. That's in the HREF of the link, and the link happens to have a target the same as the window.open() call.
What's going on, is the window.open() call gets triggered first. It creates a window with the right target and the right dimensions.
Then the link kicks in and refreshes the page to the new url. As far as the page is concerned, it isn't a popup window. That's why I got the confirmation message.
Change it to this, and putting everything in the body.onload() works just fine:
<a onclick="oW = window.open('invoice_print .php?invoi ceid=3', 'print', 'scrollbars=no,width=50,he ight=50'); oW.startPrint(); oW.close()" target="print" href="javascript:void(0)"> Print</a>
Because now the window.open actually specifies the URL.
You see my call to open the popup window:
<a onclick="oW = window.open('', 'print', 'scrollbars=no,width=50,he
Notice how the window.open() call doesn't actually have the URL in it. That's in the HREF of the link, and the link happens to have a target the same as the window.open() call.
What's going on, is the window.open() call gets triggered first. It creates a window with the right target and the right dimensions.
Then the link kicks in and refreshes the page to the new url. As far as the page is concerned, it isn't a popup window. That's why I got the confirmation message.
Change it to this, and putting everything in the body.onload() works just fine:
<a onclick="oW = window.open('invoice_print
Because now the window.open actually specifies the URL.
ASKER
Whoops the oW.startPrint() and oW.close() shouldn't be in there.
<a onclick="oW = window.open('invoice_print .php?invoi ceid=3', 'print', 'scrollbars=no,width=50,he ight=50'); " target="print" href="javascript:void(0)"> Print</a>
<a onclick="oW = window.open('invoice_print
ASKER
Also, an alternative way that I just found out is I can look at the document's readystate, and close the window from the parent page once it changes. So the printing page does start the printing from the body.onload(), but it doesn't close the window. E.g:
<a onclick="oW = window.open('invoice_print .php?invoi ceid=3', 'print', 'scrollbars=no,width=50,he ight=50');
while( oW.document.readyState != 'complete' ) { x = 1 };
window.close();" target="print" href="javascript:void(0)"> Print</a>
The x = 1 is necessary because I have to do something inside the while loop.
<a onclick="oW = window.open('invoice_print
while( oW.document.readyState != 'complete' ) { x = 1 };
window.close();" target="print" href="javascript:void(0)">
The x = 1 is necessary because I have to do something inside the while loop.
ASKER
Okay, only a B because the solution needed the change to the window.open() function to prevent the window.close() function from popping up the confirmation, which I figured out myself. But this tipped me off.
Thanks for the points, but what do you mean with "change to the window.open() function to prevent the window.close() function from popping up the confirmation" ?
Ben
Ben
<BODY onLoad="printPage()">