Can anyone tell me why this code isnt working?

rcomo

Senior member
Jan 21, 2004
227
0
0
if (strUsername == strCustomerOne && strPassword == strPasswordOne)

{
location.href = 'welcome.htm';
}

Basically, it won't open the welcome.htm file for me for some reason.

The HTML code is a web page that lets a person log in their username/password (in a form in the HTML). This statement, in Javascript, checks to see if the username/password is correct and (if it is) redirects them to the welcome.htm page.
 

Atheus

Diamond Member
Jun 7, 2005
7,313
2
0
I don't know, that looks correct, you must be failing one of the tests in the if statement so the location command does not run.

BTW - I hope this is just for learning purposes, coz you can't have a password check running client-side... that would be extraordinarily bad.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
by "isn't working" what do you mean?

FYI you can turn on scipt debugging for IE somewhere under advanced options.
 

rcomo

Senior member
Jan 21, 2004
227
0
0
Yeah this is homework hehe :) this isnt a real webpage.

By not working, it is not laoding the next page 'welcome.htm'; I know the if statement is coming up true because if I insert a different code - like a window.alert - it works.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
You still didn't say what is happening - script error (if you turned debugging on), 404 page, or nothing at all.
 

Zugzwang152

Lifer
Oct 30, 2001
12,134
1
0
make sure your variables are referenced properly, if so try "document.location='index.php'" instead of document.href
 

rcomo

Senior member
Jan 21, 2004
227
0
0
Thanks for the answer. I understand the location.href now, that is a big help.

However, changing the code to:

if (strUsername == strCustomerOne && strPassword == strPasswordOne)

{
document.location = 'welcome.htm';
}

Did not work either. Is that code wrong as well?
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
First, be sure of order of operations.

if ((strUsername == strCustomerOne) && (strPassword == strPasswordOne))

I'm guessing you want that.

This works for me in Opera 9:
document.location='welcome.htm';
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Originally posted by: DaveSimmons
You still didn't say what is happening - script error (if you turned debugging on), 404 page, or nothing at all.

For example, a 404 error page would probably mean that "welcome.htm" is either in a different folder or doesn't exist at all. FYI, on unix/linux/BSD servers capitalization matters so "Welcome.htm" refers to a different page than "welcome.htm"
 

rcomo

Senior member
Jan 21, 2004
227
0
0
Sorry. What is happening, is nothing at all.

Edit: Also, the code that I have here is cut/pasted so what you see is what you get. The page I'm trying to reference is welcome.htm.
 

Snapster

Diamond Member
Oct 14, 2001
3,916
0
0
I don't know what you are doing because you don't really say other than its not working. Below is a quick snippit I made, using the same logic and it works on IE and Mozilla quite happily.
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
try:


if (strUsername == strCustomerOne && strPassword == strPasswordOne)

{
alert("passwords match");
ocation.href = 'welcome.htm';
} else {
alert("passwords do not match");
}

if you see a message box popup then ur code is actually executing and ur test works.
also, if this is part of a sequence of code you may have a syntax error before it htat causes JS to bail out before getting to this part of the code.
Also, this code may never actually run if you haven't registered it properly as an event or placed it in the right place as a script.
 

rcomo

Senior member
Jan 21, 2004
227
0
0
Thanks Snapster. The problem I am having is the page will not reference the welcome.htm for some reason. Both firefox and IE won't do it; when you click on the button it just doesnt do anything - the original page just reloads without the information, such as username and email, in it anymore.

So the problem is getting the location.href to work in either VBScript or Javascript (I know the original was in js but I'm willing to try anything at this point.)
 

rcomo

Senior member
Jan 21, 2004
227
0
0
If this helps, after clicking the button to load the next page the URL in the browser goes from this:

C:\Documents and Settings\Owner\Desktop\HTML Projects\Chap 5\hardwareLogin.htm

to this:

file:///C:/Documents%20and%20Settings/Owner/Desktop/HTML%20Projects/Chap%205/hardwareLogin.htm?txtUsername=brian&txtPassword=key&btnLogin=Login

Edit: just to clarify, I have entered the username as brian and the password to key.

statik213: I thought of that too. I reduced the code to one simple statement (like document.write("Is this freakinng working??") and it exicuted that, so I know its soley getting location.href right now is my issue. :( Arrrggg isnt learning fun??
 

rcomo

Senior member
Jan 21, 2004
227
0
0
Ok I switched to VBScript and have this very simple code that should work but it does nothing.

<script language="VBScript">

function loginVB()

location.href="http://www.course.com"
window.alert "Welcome back!"

end function

</script>

It displays the welcome back window alert but does not load www.course.com.
 

Snapster

Diamond Member
Oct 14, 2001
3,916
0
0
From the sounds of it, your form is posting to itself correct? Using likely form method="get" action="mypage.htm"

If you are, you do realise you will have to strip out your username and password from the url, rather than the form (because that was posted) and you are no longer on the same page (technically).

Although it does sound like there is an underlying problem here. Could you try a body onload location.href=. It works fine for me in both IE and Mozilla, maybe you have a harsher security setup on your rig that doesn?t allow redirects?
 

rcomo

Senior member
Jan 21, 2004
227
0
0
You where right on Snapster. I had my button as "Submit" instead of "Button," and therefore it wasn't correctly passing the information to the webpage. So simple yet so freaking hard to debug lol.

Thanks for everyone's help.