Click to See Complete Forum and Search --> : HELP with popup window controls PLEASE
contento
08-27-2003, 12:08 PM
I'm using PayPal's shopping cart for a project. Their form that adds items to the cart opens the cart in a new window (called paypal, correct?):
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="add" value="1">
</form>
This popup window has a continue shopping button that just refocuses the opener window (not closing the shopping cart popup). The problem is that when you add the next item to the cart, the cart window does not refocus.
I would like to do one of the following:
1. have a little javascript in the opener page the closes the popup page onfocus. I tried this but it didn't work:
<body OnFocus="paypal.close()">
and a bunch of other syntaxes:
<body OnFocus="window.paypal.close()">
<body OnFocus="paypal.window.close()">
<body OnFocus="window.close(paypal)">
2. OR if I could add an OnSubmit item to the form the refocuses the popup window, that'd work as well. I don't know that syntax.
Unfortunately, I have no access to the shopping card page code so I can't just add a neat little onunload or onblur. That'd be too easy.
Thanks all!
David Harrison
08-27-2003, 01:37 PM
Here's a little pop-up controller script, play around see what you think.
Note: Event handlers are onfocus, onblur etc. not OnFocus, OnBlur.
contento
08-27-2003, 03:10 PM
for the reply. I don't have any trouble with creating popup windows of my own. What I'm struggling with is controlling the popup window that is created for me by the PayPal form above.
Thanks
David Harrison
08-27-2003, 03:15 PM
And I've included two links that close and focus the pop-up window. I'd call that controlling.
contento
08-27-2003, 03:21 PM
I saw that and again, I appreciate the effort, but unfortunately, I can't open the window with traditional methods (like what you've included).
The form handler as created by PayPal submits the action to a new window it calls paypal (and loads the PayPal files). I have no control over the page name, specs., etc.
If you read back to my original post, I'd like to do one of two things (but can't seem to nail down the syntax/function)
Thanks again.
David Harrison
08-27-2003, 03:33 PM
Well it looks like you've got it already:
<body onfocus="paypal.close()">
But I don't know whether or not the name of the window is paypal.
contento
08-27-2003, 03:37 PM
I THOUGHT my syntax should be right, but it doesn't work. The window name is paypal (defined in the target tag for the form handler):
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
I tested this by creating a second matching form with paypal2 as the target and sure enough, a second window opened.
hmmm....how about my 2nd option (an onsubmit function for the form that refocuses the window)?
David Harrison
08-27-2003, 03:44 PM
Unfortunately that would look like this:
onsubmit="paypal.focus();return true;"
dragle
08-27-2003, 04:46 PM
So ultimately you know the name of the window but don't have a reference for it, yes? You can try this:
function focusPayPal() {
var myPayPalWin = window.open('','paypal');
if((myPayPalWin)&&(myPayPalWin.focus)) myPayPalWin.focus();
}
...
onsubmit="setTimeout('focusPayPal()',500); return true;"
Theoretically it should work, but test it a bunch; I'm especially not sure of the effect of calling open with a null URL in all browsers (especially in tandem with the loading of a page via a form target). You mayt also need to tweak the timeout a bit.
Cheers,
David Harrison
08-27-2003, 04:53 PM
No the problem is that he doesn't know the name of the pop-up window.
I've already posted a simple script that will open, focus and close a pop-up but I knew the name of that one.
dragle
08-28-2003, 02:05 PM
Hmmmmmm... ok... the name of the window and its reference are two different things. If I'm reading the above right, you do know the name of the window, since you are opening it via a target attribute. I.E., either of these links will open the page in a new window that is named "paypal":
<a href="blah.html" target="paypal" ...
<form target="paypal" ...
So if that's what you have in the page then you know the window that's being opened is named "paypal."
But just knowing that name is not the same thing as having a reference to the window object itself, which is what you need if you want to call its focus() method. I.E., because the window is named paypal does not mean you can use statements like this:
paypal.close();
paypal.focus();
etc. You must first retrieve a reference to the window object, then you can use that reference in the statements above. That's where window.open comes in handy, as it will return a reference to the existing window (assuming it already exists; and if it doesn't, it is opened) if you know its name and include that name as part of the window.open command; i.e.
var myPayPalWin = window.open('','paypal');
Further, theoretically (and this corresponds to the theoretically in my original message) the blank URL as the first parameter of the window.open should prevent the browser from actually altering the current contents of the window itself, so you shouldn't lose what's already in there.
Then, once you have the reference to the window object via the code above, you can use the focus method with it:
var myPayPalWin = window.open('','paypal');
myPayPalWin.focus();
Or perhaps this might explain it better. Assuming you have this link in your page:
<a href='aPage.html' target='foo'>Click Me</a>
and you click it to open a new window and then return to execute this code in the opening window:
alert(typeof(foo)); // undefined
var bar = window.open('','foo');
alert(typeof(bar)); // object
alert(bar.name); // foo
Of course, processing certain actions on the window object will generate security errors if the page that was loaded into the window originally is from a different domain as the page in the opener; but as far as I remember focus isn't one of them.
HTH,