Solved

cookie value in VBScript and JavaScript

Posted on 2003-10-25
8
913 Views
Last Modified: 2009-07-29
Hi All,

In my web application, I'm creating some cookie using VBScript and later I'm reading those cookie using JavaScript.

The problem is whenever there space in cookie value, the space is getting converted to "+" when I read the same cookie in javascript code.

For example in the below code the "LastName MiddleName FirstName" cookie value is getting printed as "LastName+MiddleName+FirstName" when I read the cookie value using JavaScript.

How can I avoid this ? Please note that  unescape(document.cookie) and document.cookie returns the same value in the current example

Thanks,
Lilupat

-----------------------------------------------------------------------------------------
<%@ Language=vbScript%>
<%
     'create a cookie in VBScript
     Response.Cookies("name") = "LastName MiddleName FirstName"
     Response.Cookies("name").Path = "/"
      
Response.Write("In VBScript Cookie Value: " & Request.Cookies("name"))

%>

<html>
<head>
<script language="javascript">
   alert("In JavaScript Cookie Value:" + unescape(document.cookie));
</script>
</head>
</html>
-------------------------------------------------------------------------------------------
0
Comment
Question by:lilupat
  • 5
  • 3
8 Comments
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 9622154
lilupat,
Why don't you just use Replace() function to eliminate the extra "+" sign?
Eg:
VBScript
----------
<%
..
Response.Write("In VBScript Cookie Value: " & Replace(Request.Cookies("name"),"+"," ")
%>

Javascript
-----------
<script language="javascript">
   alert("In JavaScript Cookie Value:" + (document.cookie).replace('+',' ');
</script>

Regards
x_com
0
 

Author Comment

by:lilupat
ID: 9622942
Replace function won't help me .......... what if I need "+" value in my cookie value ?

0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 9623044
lilupat,
Then, you need to use escape() before store any value into the cookies. When you need to retrieve, you can use unescape(). This sure be work for you.

Regards
x_com
0
 

Author Comment

by:lilupat
ID: 9624610
Hi x_com,

That won't help me either.

As I know we should not escape the cookie value when we create a cookie in VBScript because ASP(script) automatically takes care escaping the same. One more thing if I escape the value in VBScript, how I will unescape the same in Javascript.

For example if I create a cookie value in VBScript like
Response.Cookies("test") = escape("What is your FirstName?")

Then if I read the same in JavaScript, the value I get after unescape the cookie value is "what%20is%20your%20FirstName%3F"

Regards,
LiluPat

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 9624652
lilupat,

If you want to store the value for lastname, middlename, firstname,
why don't you just store the value into this way in cookies?
Eg:

Response.Cookies("user")("lastName") = "LastName"
Response.Cookies("user")("middleName") = "MiddleName"
Response.Cookies("user")("firstName") = "FirstName"

Regards
x_com
 
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 9624703
lilupat ,
>>VBScript
-You need to do like this :
<%
 'vbscript
 Response.Cookies("user")=escape("lastname middlename firstname")
 Response.Write(unescape(Request.Cookies("user")))  
%>

if you set cookies use vbscript, when you retrieve the cookies value in jscript, you need to replace() the cookies with "%" to " "
or

if you set cookies use jscript, when you retrieve the cookies value in vbcript, you need to replace() the cookies with "+" to " "

But, my best advice is you try to store the value into an array in single cookies, like this :
<%
Response.Cookies("user")("lastName") = "LastName"
Response.Cookies("user")("middleName") = "MiddleName"
Response.Cookies("user")("firstName") = "FirstName"
%>

-this method sure be work for vbscript and jscript without doing any extra job for escape() or unescape().

Regards
x_com
0
 

Author Comment

by:lilupat
ID: 9625164
I got an idea, instead of creating the cookie in VBscript, I can set the same in Javascript inside the ASP page, like

Response.write("<script language='Javascript'>")
Response.write("document.cookie ='cookie_name=" & cookie_value & "'")
Response.write("</script>")

I think this will solve my problem.

Thanks x_com for replying to my query.

Lilupat



0
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 50 total points
ID: 9625243
you're welcome, lilupat.

x_com
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Query Join 9 61
Automatically log-in a new user through code 1 55
Css expand Div to fit its content (Of other divs) 4 46
SMS AddAttachment Protocol Error Classic ASP 10 36
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now