location.href--what am i doing wrong?

D

David Goldstein

Hello, all,
I am new to this group and have a question that I am sure one of you JS
experts can quickly help me with. I have a login page that calls a php
script to verify that the user exists. If the user exists, the value
returned is "success" and the user should be directed to a member page.
Well, everything is working except for the redirect. Here is the code in
question:

--------------------------------------------------------
function updatePage() {
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;
if (response == "success") {
alert(response+"! member exists");
window.location.href = "../memberPage.php";
return false;
} else {
document.getElementById("testDiv").value = response;
alert(response);
}
}
}
-----------------------------------------------------------

I have tried location.href and window.location.href, however, nothing is
working. The value being returned is correct and the alert box displays
with the success message. The next line does not execute, however.

Thank you in advance,
David
 
S

Sam Kang

David said:
returned is "success" and the user should be directed to a member page.
Well, everything is working except for the redirect.
if (response == "success") {

Are you sure your servlet is returning only the string "success"? Without "\n"
or "\r" oder "\r\n"? In that case you can't compare the response. Try a
replacement of leading and trailing white spaces. i.e.

if (response.replace(/^\s+/, '').replace(/\s+$/, '') == "success")

or similar.

Did you checked it with a debugger?

Sam
 
D

David Goldstein

David Goldstein said:
Hello, all,
I am new to this group and have a question that I am sure one of you JS
experts can quickly help me with. I have a login page that calls a php
script to verify that the user exists. If the user exists, the value
returned is "success" and the user should be directed to a member page.
Well, everything is working except for the redirect. Here is the code in
question:

--------------------------------------------------------
function updatePage() {
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;
if (response == "success") {
alert(response+"! member exists");
window.location.href = "../memberPage.php";
return false;
} else {
document.getElementById("testDiv").value = response;
alert(response);
}
}
}
-----------------------------------------------------------

I have tried location.href and window.location.href, however, nothing is
working. The value being returned is correct and the alert box displays
with the success message. The next line does not execute, however.

Thanks to all who suggested a solution. I feel like an idiot, but I will
explain what happened :-0 The page being called is checking for a PHP
session variable that the user actually logged in. If the user did not log
in, he is sent to the index page. Well, the string that I was checking for
on the member page ("TRUE") was spelled differently than the string that I
had passed ("true").

I figured out the problem when I tried to go directly to the memberPage
and was immediately sent to the index page. Everything was working as it
should, so, I checked that the session variable was spelled correctly and
then saw that I checked against an incorrect spelling :-(((

There were no error messages, since the JS, and the PHP script were both
doing what they were supposed to do. I guess I should be mire careful when
programming late in to the evening.

Again, thanks to all for their positive feedback.

David
 
E

Evertjan.

Hans-Georg Michna wrote on 14 nov 2009 in comp.lang.javascript:
If you want to remove whitespace, at least do it efficiently:

if (response.replace(/^\s+|\s+$/g, "") === "success") ...

I doubt though that this is actually the problem.

Efficiently?

Try:

if (/^\s*success\s*$/.test(response)) ....
 
E

Evertjan.

Hans-Georg Michna wrote on 15 nov 2009 in comp.lang.javascript:
Ah, great! I overlooked that.

It has happened to me time and again that I improved some
JavaScript code, only to find later that it could be improved
even more. So how about this?

if (/success/.test(response)) ...

Of course this works only if you can be sure that no other
possible response contains "success", which is very likely,
however. :)-)

I don't think that argument is valid, or you could do:

if (/ucce/.test(response))

if you can be sure that no other
possible response contains "ucce", which is very likely,
if all the other options do not include success or part of that word.

However

response = 'sorrynosuccess';

id not an unlikely alternative.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top