Asp:
<%setcookie ("user_id", "Test")%>//Pass a cookie, name: user_id value: Test
Javascript:
<script>
Alert (GetCookie ("user_id")); Read results show null Halo!
</script>
Use Firefox to view cookies and find that cookies are a bit different.
Cookie name: user_id changed to User%5fid
Why does the name change? I went to Baidu search, find the reason:
Http://bbs.chinaunix.net/thread-743431-1-1.html
It turns out that when an ASP uses UTF-8 encoding, the cookie written to the browser is UrlEncode encoded.
The solution is to decode in javascript: decodeuricomponent
Modified JavaScript code:
Set a cookie with the name named value
function Setcookie (name, value)
{
var argc = setCookie.arguments.length;
var argv = setcookie.arguments;
var path = (argc > 3)? ARGV[3]: null;
var domain = (argc > 4)? ARGV[4]: null;
var secure = (argc > 5)? ARGV[5]: false;
Document.cookie = name + "=" + value + (path = = null)? "" : ("; Path= "+ path") + ((domain = = null)? "" : ("; domain= "+ domain") + ((secure = = True)? "; Secure ":");
}
Delete a cookie named name
function Deletecookie (name)
{
var exp = new Date ();
Exp.settime (Exp.gettime ()-1);
var cval = GetCookie (name);
Document.cookie = name + "=" + Cval + "; Expires= "+ exp.togmtstring ();
}
Gets the value of the cookie that is called offset by the item name
function Getcookieval (offset)
{
var cookie_content = decodeuricomponent (Document.cookie);
var endstr = Cookie_content.indexof (";", offset);
if (endstr = =-1) endstr = cookie_content.length; Return unescape (cookie_content.substring (offset, endstr)); }
Get the cookie value named name
function GetCookie (name)
{
var arg = name + "=";
var alen = Arg.length;
var cookie_content = decodeuricomponent (Document.cookie);
var clen = cookie_content.length;
var i = 0;
while (I < Clen)
{
var j = i + Alen;
if (Cookie_content.substring (i, j) = = arg)
Return Getcookieval (j);
i = Cookie_content.indexof ("", I) + 1;
if (i = = 0) break;
}
return null;
}
ASP (UTF-8) set cookie reads a cookie with JavaScript and discovers a problem