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

Problem with displaying empty string

Status
Not open for further replies.

Goalie3533

Programmer
Apr 14, 2004
53
US
Hi guys.
I'm now having a problem trying to display an empty string in javascript. I successfully retrieve 2 key values from a cookie(first & last name) and display them on the page. That part works fine. The error occurs when it is a new user who doesn't have a cookie yet. I have code set up to check if firstname & lastname are equal to nothing to print something different but for some reason, even though they should be empty strings, the value "Ca" is populating both of them. Here's the portion of my code giving me problems. It's code for a user login. fname & lname are the ones I'm using to store my cookie values:
------------------------------------
strWelcome="<b>Welcome " + fName + " " + lName + "</b> | ";

if(fname = "")
{
document.write("<a href='../custlogin.asp' alt='Log into your account'>Login</a>");
}
else
{
document.write(strWelcome + "<a href='../logout.asp' alt='Logout of your account'>Logout</a>");
}
------------------------------------------
So what should be happening is if the user has no cookie, it should simply provide a link containing the word "Login", but unfortunately, it ends up reading "Welcome Ca Ca | Logout". I have no idea where this "Ca Ca" is coming from.

Any idea how I can fix this?
Thanks in advance.

-Goalie35
 
instead of
Code:
if(fname = "")

have you tried
Code:
if(!fname)
 
Another problem you are having could be a logic error.

if(fname = "") is an assignment statement (unlike VB).

if(fname == "") is a comparison statement.
 
I actually tried both of those but for some strange reason, when my page encountered this code, it didn't like it and would stop reading the remainder of the javascript on the page. It wouldn't print either of the 2 document.write's in my if statements and it ignored my "alert" message that I called right after these if statements.
 
Can you display the rest of the code. Most important is the code that checks for the cookie.
 
Sure.
This is all of it. The top portion reads my cookie string and finds out where my first & last name keys start and end within my cookie string and sends those values to the fname & lname variables. The way my code is set up though is that my cookie is actually removed from the users system(I do this on my "logout.asp" page) when they logout so since there is no cookie to be found, I don't understand where this "Ca Ca" is coming from.
------------------------------------
<script>
var str, strWelcome;
var fNameStart, fNameEnd, fName;
var lNameStart, lNameEnd, lName;

str = document.cookie;

fNameStart = str.indexOf("%3D") + 3
fNameEnd = str.indexOf("%2C")
fName = str.substring(fNameStart,fNameEnd)

lNameStart = str.indexOf("%3D",fNameEnd) + 3
lNameEnd = str.indexOf("%2C",lNameStart)
lName = str.substring(lNameStart, lNameEnd)

strWelcome = "<b>Welcome " + fName + " " + lName + "</b> | ";

if(fname = "")
{
document.write("<a href='../custlogin.asp' alt='Log into your account'>Login</a>");
}
else
{
document.write(strWelcome + "<a href='../logout.asp' alt='Logout of your account'>Logout</a>");
}
</SCRIPT>
-------------------------------------------
Thanks again for any help you can provide.

-Goalie35
 
It seems that it is still finding something that starts with Ca. Instead of trying to determine what is actually happening, you would probably be best served changing the functionality. Go to this site:
It has functions that you can copy/paste into your page that make it extremely easy to obtain cookies. Why try to reinvent the wheel?

The GetCookie and getCookieVal functions should be of use to you if nothing else.

Hope this helps.
 

>> The way my code is set up though is that my cookie is actually removed from the users system(I do this on my "logout.asp" page) when they logout

How does your cookie deletion code work? Are you sure that it isn't populating "Ca Ca" into the cookie?

What do you get if you alert "document.cookie" before and after the "deletion"? (Hopefully, not a load of Ca Ca ;o)

Hope this helps,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top