?
Solved

Open popup, then call javascript function in the popup

Posted on 2008-02-02
10
Medium Priority
?
2,390 Views
Last Modified: 2013-12-12
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?
0
Comment
Question by:Frosty555
  • 5
  • 3
  • 2
10 Comments
 
LVL 3

Expert Comment

by:bigdaddyz99
ID: 20804744
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
 
LVL 7

Accepted Solution

by:
BenMorel earned 750 total points
ID: 20804753
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
 
LVL 31

Author Comment

by:Frosty555
ID: 20804775
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 7

Assisted Solution

by:BenMorel
BenMorel earned 750 total points
ID: 20804825
AFAIK, popup windows opened with window.open() don't ask confirmation before closing !
Regards,
Ben
0
 
LVL 3

Expert Comment

by:bigdaddyz99
ID: 20804832
How about putting the window.close at the end of the printPage() function?
0
 
LVL 31

Author Comment

by:Frosty555
ID: 20804897
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
 
LVL 31

Author Comment

by:Frosty555
ID: 20804921
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
 
LVL 31

Author Comment

by:Frosty555
ID: 20804926
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
 
LVL 31

Author Closing Comment

by:Frosty555
ID: 31427475
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
 
LVL 7

Expert Comment

by:BenMorel
ID: 20804963
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

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question