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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

window.location.replace doesn't work inside function

Status
Not open for further replies.

altaratz

ISP
Apr 15, 2001
73
0
0
US
I'm trying to trigger 1 of 2 different cgi scripts based on the username that the person enters into a form - everything seems to be working fine (the user-specific url stings are getting built just fine) in the below code, except that when I try to do window.location.replace from inside one of the if statements of the pickdirection()function, nothing happens . . . why oh why is this . . .

<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="GENERATOR" CONTENT="Mozilla/4.01 [en] (Win95; I) [Netscape]">
<TITLE>Highway M Client Login</TITLE>



</HEAD>
<FORM method=post onSubmit="return pickdirection(form1)" name="form1" >

<table border=0>
<tr><td>
Username</td><td><INPUT type=text name="user" size=20></td></tr>
<tr><td>
Password</td><td><INPUT type=password name="password" size=20></td></tr>
</table>
<p>

<INPUT type="Submit" name="pass" Value=" Click to Login ">
</FORM>

<script>
function pickdirection(form) {
var url_2;
var user_variable;
user_variable = form.user.value;
var password_variable;
password_variable = form.password.value;
if (form.user.value == "mrussell"){
window.location.replace(" alert("got here");}
else if (form.user.value != "mrussell") {
url_2 = " alert(url_2);
}
}
//-->
</script>


</BODY>
</HTML>
 
just change the
Code:
 else if (form.user.value != "mrussell") {
to
Code:
 else {
 
Actually - the thing is, even when the value is mrussell, the window.location.replace statement won't fire off.

Try it - goto
in both cases - either with an entered username of mrussell or not, the function should jump the browser to - but for some reason it's not :-(

here's the code again - though you can view it yourself at the above url

<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="GENERATOR" CONTENT="Mozilla/4.01 [en] (Win95; I) [Netscape]">
<TITLE>Highway M Client Login</TITLE>



</HEAD>
<FORM method=post onSubmit="return pickdirection(form1)" name="form1" >

<table border=0>
<tr><td>
Username</td><td><INPUT type=text name="user" size=20></td></tr>
<tr><td>
Password</td><td><INPUT type=password name="password" size=20></td></tr>
</table>
<p>

<INPUT type="Submit" name="pass" Value=" Click to Login ">
</FORM>

<script>
function pickdirection(form) {
var url_2;
var user_variable;
user_variable = form.user.value;
var password_variable;
password_variable = form.password.value;
if (form.user.value == "mrussell"){
window.location.replace(" alert("nothing happened");}
else if (form.user.value != "mrussell") {
window.location.replace(" alert("nothing happened");
}
}
//-->
</script>


</BODY>
</HTML>
 
guess IE is just bad or something - I still haven't gotten it to jump to yahoo . . .
 
try this version
Code:
<script>
<!--
  function pickdirection() {
  var url_2;
  var user_variable;
  user_variable = document.getElementById('user').value;  
  var password_variable;
  password_variable =document.getElementById('password').value;
  if (user_variable == "mrussell"){
  //window.location.replace("[URL unfurl="true"]http://www.yahoo.com")[/URL]
  alert("worked");}
  else {
  url_2 = "[URL unfurl="true"]http://www.domain.com/cgi-bin/download.cgi?user="+user_variable+"&password="+password_variable;[/URL]
  alert(url_2);
    }
  }
//-->
</script>
 
This works on my computer in both IE 6 and Firefox:

<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="GENERATOR" CONTENT="Mozilla/4.01 [en] (Win95; I) [Netscape]">
<TITLE>Highway M Client Login</TITLE>
</HEAD>
<body>
<FORM method=post onSubmit="return false" name="form1" >
<table border=0>
<tr><td>
Username</td><td><INPUT type=text name="user" size=20></td></tr>
<tr><td>
Password</td><td><INPUT type=password name="password" size=20></td></tr>
</table>
<p>

<INPUT type="button" name="pass" value=" Click to Login " onclick="pickdirection('form1');">
</FORM>

<script>
function pickdirection(form)
{
var user_variable = document.forms[form].elements['user'].value;
var password_variable = document.forms[form].elements['password'].value;

if (user_variable == "mrussell")
{
window.location.replace(" alert("nothing happened");
}
else
{
window.location.replace(" alert("nothing happened 1");
}
}
//-->
</script>
</BODY>
</HTML>


Lee
 
Do you really need to use "window.location.replace"? Couldn't you just use "window.location = new-URL"?


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
sorry i left the form part out

Code:
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="GENERATOR" CONTENT="Mozilla/4.01 [en] (Win95; I) [Netscape]">
   <TITLE>Highway M Client Login</TITLE>

   
<script>
<!--
  function pickdirection() {
  var url_2;
  var user_variable;
  user_variable = document.getElementById('user').value;  
  var password_variable;
  password_variable =document.getElementById('password').value;
  if (user_variable == "mrussell"){
  //window.location.replace("[URL unfurl="true"]http://www.yahoo.com")[/URL]
  alert("worked");}
  else {
  url_2 = "[URL unfurl="true"]http://www.domain.com/cgi-bin/download.cgi?user="+user_variable+"&password="+password_variable;[/URL]
  alert(url_2);
    }
  }
//-->
</script>
   
</HEAD>
<FORM method=post onSubmit="return pickdirection()" name="form1" >

<table border=0>
<tr><td>
Username</td><td><INPUT type=text name="user" size=20 id="user"></td></tr>
<tr><td>
Password</td><td><INPUT type=password name="password" size=20 id="password"></td></tr>
</table>
<p>

<INPUT type="Submit"  name="pass" Value=" Click to Login ">
</FORM>

</BODY>
</HTML>
 
Along the lines of Tracy's question, I use redirection on a separate page that does the validation rather than doing the validation on the login page itself. Of course, client-side validation is pretty weak, so I don't use the method in the above example.

Lee
 
i agree troll but the original question wasn't to give a better method it was to fix the problem.

i myself would use a server-side script, securing the sub pages with sessions
 
thanks so much for the overwhelming responses - I'll just call it a brain fart and be done with it.

Trollacious' solution worked great - still eluding me why mine didn't work, but whatever.

Thanks again!
 
still eluding me why mine didn't work

At a guess (I haven't tested this theory), I would say it was not your function, but the way you were calling it:

Code:
<FORM method=post onSubmit="return pickdirection(form1)" name="form1">

I would have used:

Code:
<FORM method=post onSubmit="return pickdirection([b]this[/b])" name="form1">

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top