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?
LVL 31
Frosty555Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bigdaddyz99Commented:
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()">
0
BenMorelCommented:
Hi,
Maybe that's one of IE's famous "access denied" issues.
Moreover, maybe the window is not loaded yet when the startPrint() is called from the main page.

Try to just open the popup from the main page, then change your body in the popup :

<body onload="startPrint(); window.close();">

Regards,
Ben
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Frosty555Author Commented:
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.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

BenMorelCommented:
AFAIK, popup windows opened with window.open() don't ask confirmation before closing !
Regards,
Ben
0
bigdaddyz99Commented:
How about putting the window.close at the end of the printPage() function?
0
Frosty555Author Commented:
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.
0
Frosty555Author Commented:
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>
0
Frosty555Author Commented:
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.
0
Frosty555Author Commented:
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.
0
BenMorelCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.