Forum Moderators: open
I have a real problem with an e-commerce site which has worked for years without a hitch but with the uptake of IE8 (it seems) this problem is getting more and more frequent.
Problem: customer continues through checkout on main site (http://www.example.com/checkout.php).
When they have to enter their credit card details, they click on a link to open a window (https://www.example.com/cc.php). The details are verified and if valid, the customer is returned to the confirmation page in the parent screen (http://www.example.com/thanks.php) and the payment window is closed.
Pop-up blockers are causing problems here but even when pop-up blockers are disabled, the confirmation page loads in a new window.
I'm using :
<script language="JavaScript">
<!--
window.opener.location="<?php echo ($main_url);?>thanks.php>";
window.close();
-->
</script>
I've tried as a test:
<a href="#" onclick="window.opener.location.href ='<?php echo ($main_url);?>thanks.php?'; window.close(); ">Click HERE</a>
But the page always loads in a new window and not the parent window.
Is there any way to load the confirmation page in the parent window?
Help appreciated, thanks.
[edited by: DrDoc at 4:44 am (utc) on June 9, 2009]
[edit reason] examplified URIs [/edit]
You have two different errors, I think.
$main_url='http://www.example.com/';
In the JS
window.opener.location="<?php echo ($main_url);?>thanks.php>";
would give you
window.opener.location="http://www.example.com/thanks.php>";
Note the errant >
When what you want is
window.opener.document.location="http://www.example.com/thanks.php";
In your inline link, you have
onclick="window.opener.location.href ='<?php echo ($main_url);?>thanks.php?'; window.close(); "
which gives you
onclick="window.opener.location.href ='http://www.example.com/thanks.php?'; window.close();"
When what you want is
onclick="window.opener.document.location='http://www.example.com/thanks.php'; window.close(); return false;"
Return false stops the link from performing it's natural action, navigating to # (top of page.) Add return false to your function too, which allows you to put the real URL in the link if JS is disabled. A couple other changes for legacy eradication:
<script type="text/javascript">
function TY_page(url) {
window.opener.document.location=url;
window.close();
return false;
}
</script>
....
<p><a href="<?php echo "$main_url/thanks.php";?>"
onclick="return TY_page('<?php echo "$main_url/thanks.php";?>');">Finish Checkout</a> </p>
Note "Finish Checkout" - never use text in links to describe how to use the document. Some user's can't "click" and click here is one of the web's "Dangerous Words".
Legacy:
<script language="JavaScript">
=
<script type="text/javascript">
<!--
-->
= Not needed
^^^ This was used to hide Javascript from unsupported browsers.
<a href="#" onclick="window.opener.location.href ='<?php echo ($main_url);?>thanks.php?'; window.close(); ">Click HERE</a>
=
<a href="<?php echo "$main_url/thanks.php";?>"
onclick="return TY_page('<?php echo "$main_url/thanks.php";?>');">Finish Checkout</a>
# will just navigate to the page top; a real URL here and returning false in the onClick allows Javascript to manage the click; if JS is disabled the content is still accessible. Second, moving the actual actions to a function compartmentalizes the functions and makes the HTML easier to use. A further step would be to attach behaviors to this link in a window.onload() { }; function, rather than having an inline event handler.
"Click here"
=
Finish Checkout
Never use page text to describe the document mechanics; although it's not directly Javascript, it's still a "legacy" or "old school" waste of space.