Link to home
Start Free TrialLog in
Avatar of Frosty555
Frosty555Flag for Canada

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,height=50'); oW.startPrint(); oW.close()" target="print" href="invoice_print.php?invoiceid=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.visibility = 'hidden';
      ScriptXPrint();
      setTimeout('document.all.print.style.visibility = \'visible\'', 500);
}
</script>
...
</head>


Can anyone help me make this function call work?
Avatar of bigdaddyz99
bigdaddyz99
Flag of United States of America image

Instead of trying to call the function from your list page, put an onload event on the print page that will automatically call it when it loads

<BODY onLoad="printPage()">
ASKER CERTIFIED SOLUTION
Avatar of BenMorel
BenMorel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Frosty555

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How about putting the window.close at the end of the printPage() function?
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,height=50'); oW.startPrint(); oW.close()" target="print" href="invoice_print.php?invoiceid=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?invoiceid=3', 'print', 'scrollbars=no,width=50,height=50'); oW.startPrint(); oW.close()" target="print" href="javascript:void(0)">Print</a>

Because now the window.open actually specifies the URL.
Whoops the oW.startPrint() and oW.close() shouldn't be in there.
<a onclick="oW = window.open('invoice_print.php?invoiceid=3', 'print', 'scrollbars=no,width=50,height=50');" target="print" href="javascript:void(0)">Print</a>
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?invoiceid=3', 'print', 'scrollbars=no,width=50,height=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.
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.
Avatar of BenMorel
BenMorel

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