Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening page in same window with window.open 3

Status
Not open for further replies.

bmjavakid

MIS
Jun 8, 2004
7
US
I have a form which gives users 4 choices, based on which radio button they have selected i want to open a page in the same window. but I am having trouble getting this to work. Currently, I can only open new windows and cannot open the pages in the same window the way I want to. here is my code:

if(var=="xxx"){
window.open('xxx.html');
} else if(paychoice=="yyy") {
window.open('yyy.html');
} else if(paychoice=="zzz"){
window.open(zzz.html');
} else if(paychoice=="aaa") {
window.open(aaa.html');
} else {}
}

can anyone help me?
 
use window.location = "blah.html" instead of window.open

-kaht

banghead.gif
 
Thank you for the tip, unfortunately that still did not work. I am probably doing something wrong, but have stared at it for so long I cannot actually tell. Here is the rest of my code/form

function validatePayType()
{
var paychoice;
for (var i=0; i< paytype.payment.length; i++) {
if (paytype.payment.checked)
paychoice = paytype.payment.value;}

if(paychoice=="checking"){
window.location="Authentication.html";
} else if(paychoice=="money_market") {
window.open('Authentication.html');
} else if(paychoice=="creditcard"){
window.open('cc_Authentication.html');
} else if(paychoice=="bank_account") {
window.open('Bank_profile.html');
} else {}
}
</script>

<form name="paytype">
blah blah blah
<input type=image src="Images/btn_makepayment.gif" onClick="validatePayType()">
 
First off, I'd change all of your window.open statements instead of just the first one. Secondly I'd check which radio button is checked, instead of comparing string values when making comparisons. Try something like this:

This assumes all radio buttons are in the same order as the comparisons listed in your function
Code:
function validatePayType(obj) {
   if(obj[0].checked) {window.location = "Authentication.html";}
   if(obj[1].checked) {window.location = "Authentication.html";}
   if(obj[2].checked) {window.location = "cc_Authentication.html";}
   if(obj[3].checked) {window.location = "Bank_profile.html";}
}
and then call the function like this:
Code:
<input type=image src="Images/btn_makepayment.gif" onClick="validatePayType(document.forms['paytype'].payment)">
And just out of curiosity since you say that window.location is still opening a new window for you, try pasting this into a new html file and let me know what happens (this is about as stripped down as it gets):
Code:
<html>
<body>
<form name=blahForm>
<input type=button value='Click Me' onclick='window.location="[URL unfurl="true"]http://www.google.com"'>[/URL]
</form>
</body>
</html>

-kaht

banghead.gif
 
I guess "payment" is name of the radio buttons, you are testing by choosing the button with value "checking" and "checking" is all lower-case, and "Authentication.html" is in the same directory as the page with the radio buttons on it... yes?

What IS the html doing when you click on the image?

--Dave
 
Once again, I really appreciate your help.

I should clarify, when I changed to window.open command to window.location it does not open in a new window, nothing happens. you can see that the form information is being passed, but nothing actually happens.

I tried to change everything to what you suggested, and when I executed the command nothing happens, other than the information being passed which you can see in the url. This leads me to believe that I must have some small stupid syntax error somewhere.

As far as the google test, that worked perfectly. If only everything worked out that easily.

perhaps something is wrong with the way i have my radio buttons set up. this is currently what it looks like:

<form name="paytype">
<td colspan="5"><input type="radio" name="payment" value="checking"> Checking at Bank One</td></tr>
<tr>
<td colspan="5"><input type="radio" name="payment" value="money_market"> Money Market at Bank One</td></tr>
<tr>
<td colspan="5"><input type="radio" name="payment" value="creditcard"> Credit Card</td></tr>
<tr>
<td colspan="5"><input type="radio" name="payment" value="bank_account"> New Bank Account</td>
</tr>
<tr>
<td><input type=image src="Images/btn_makepayment.gif" onClick="validatePayType(document.forms['paytype'].payment)">
</td></form>
 
The IMAGE as INPUT-type acts like a SUBMIT button. Rather than programming its ONCLICK event to call the function, set the FORM's ONSUBMIT event as "return validatePayType()".

Code:
<form name="paytype" [b]onsubmit="return validatePayType()"[/b]>

Then, put a "return false" at the end of the function.

Code:
function validatePayType()
{
   var paychoice;
   for (var i=0; i< paytype.payment.length; i++) {
     if (paytype.payment[i].checked)        
   paychoice = paytype.payment[i].value;}
 
   if(paychoice=="checking"){
           window.location="Authentication.html"; 
   } else if(paychoice=="money_market") {
        window.open('Authentication.html');
   } else if(paychoice=="creditcard"){
           window.open('cc_Authentication.html');
    } else if(paychoice=="bank_account") {
        window.open('Bank_profile.html');
        } else {}
 [b]return false;[/b]
}

This solves the problem for me in IE6.

'hope it works for you.

--Dave
 
By default, an image acts as a submit button for a webpage. The reason you're not navigating to a new page is because when the image is clicked the page is submitting to itself. To easily fix this, change your form line to this and all should be fine:
Code:
<form name="paytype" onsubmit="return false">

-kaht

banghead.gif
 
Yes, kaht, but your solution requires fewer changes to the original HTML. High five.

--Dave
 
LookingForInfo, I awarded you a star for your helpful tips. I'm sure that bmjavakid simply forgot to do it.


-kaht

banghead.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top