lilupat
asked on
cookie value in VBScript and JavaScript
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+First Name" 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").P ath = "/"
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>
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- -----
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+First
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").P
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>
--------------------------
ASKER
Replace function won't help me .......... what if I need "+" value in my cookie value ?
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
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
ASKER
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%20FirstN ame%3F"
Regards,
LiluPat
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%20FirstN
Regards,
LiluPat
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
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")("
Response.Cookies("user")("
Response.Cookies("user")("
Regards
x_com
lilupat ,
>>VBScript
-You need to do like this :
<%
'vbscript
Response.Cookies("user")=e scape("las tname middlename firstname")
Response.Write(unescape(Re quest.Cook ies("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
>>VBScript
-You need to do like this :
<%
'vbscript
Response.Cookies("user")=e
Response.Write(unescape(Re
%>
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")("
Response.Cookies("user")("
Response.Cookies("user")("
%>
-this method sure be work for vbscript and jscript without doing any extra job for escape() or unescape().
Regards
x_com
ASKER
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.c ookie ='cookie_name=" & cookie_value & "'")
Response.write("</script>" )
I think this will solve my problem.
Thanks x_com for replying to my query.
Lilupat
Response.write("<script language='Javascript'>")
Response.write("document.c
Response.write("</script>"
I think this will solve my problem.
Thanks x_com for replying to my query.
Lilupat
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why don't you just use Replace() function to eliminate the extra "+" sign?
Eg:
VBScript
----------
<%
..
Response.Write("In VBScript Cookie Value: " & Replace(Request.Cookies("n
%>
Javascript
-----------
<script language="javascript">
alert("In JavaScript Cookie Value:" + (document.cookie).replace(
</script>
Regards
x_com