javascript asp教程第九课--cookies(2)

Click Here to run the script in a new window.

Do I even need to explain "firstCookie" and "secondCookie"? It's so easy. However, I will have to have explain the retrieval of Keys in "thirdCookie".

We retrieve cookies in almost exactly the same way that we set them, and that goes for Keys as well. If you keep track of your Key names, then retrieving their values is pretty easy.

var thirdCookieFirstKey = Request.Cookies("thirdCookie")("firstKey")

Deleting Cookies:

To delete a cookie, give it an expiration date in the past. The following is not in our examples, but it would work.

var Yesterday=new Date()
Yesterday.setDate(Yesterday.getDate() - 1)
myExpire = (Yesterday.getMonth() + 1) + "/" + Yesterday.getDate() 
myExpire += "/" + Yesterday.getFullYear()
Response.Cookies("firstCookie").Expires=myExpire

Cookie Crumbs:

What if you lose track of your cookie names? What if you lose your Keys? First of all, don't do that. But secondly, we have options.

We can use a VBScript Function with a for/each loop. We can use Javascript's new Enumerator( ) or we can use a pair of for Loops. We, however, cannot use JavaScript's for/in loop. This means we have to be creative.

<%@LANGUAGE="JavaScript"%>
<%
if (Response.Cookies.Count <= 0)
	{
	Response.Redirect("script09.asp")
	}
%>
<HTML>
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Function forEachCookie()
dim x,y,z
for each x in Request.Cookies
	if Request.Cookies(x).HasKeys then
		for each y in Request.Cookies(x)
		z = z & x & ":" & y & "=" & Request.Cookies(x)(y) & "; "
		next
	else
		z = z & x & "=" & Request.Cookies(x) & "; "
	end if
next
forEachCookie = z
End Function
</SCRIPT>
<%
Response.Write("<STRONG>Let's use a VBScript function to ")
Response.Write("sort out the Cookies and Keys.</STRONG><BR>\r")
var longCookie = forEachCookie()
if (longCookie)
	{
	longCookie = longCookie.split("; ")
	for (i=0;i<=longCookie.length-1;i++)
		{
		Response.Write(longCookie[ i ] + "<BR>\r")
		}
	}
Response.Write("<HR>\r")

Response.Write("<STRONG>Let's use <I>new Enumerator( )</I> to ")
Response.Write("sort out the Cookies and Keys.</STRONG><BR>\r")
var myEnum = new Enumerator(Request.Cookies);
for (myEnum; !myEnum.atEnd() ; myEnum.moveNext() )
	{
	Response.Write(myEnum.item() + "=")
	n=Request.Cookies(myEnum.item()).Count
	if (n)
		{
		for (o=1;o<=n;o++)
			{
			Response.Write(Request.Cookies(myEnum.item())(o) + " ")
			}
		//Begin alternate method of using Enumerator()
		Response.Write("<BR>\r<STRONG>OR... </STRONG>")
		Response.Write(myEnum.item() + "=") 
		Response.Write(unescape(Request.Cookies(myEnum.item())))
		//End alternate method
		}
	else
		{
		Response.Write(Request.Cookies(myEnum.item()))
		}
	Response.Write("<BR>\r")
	}
Response.Write("<HR>\r")

Response.Write("<STRONG>Let's use a pair of JavaScript loops to ")
Response.Write("sort out the Cookies and Keys.</STRONG><BR>\r")
a=Request.Cookies.Count
for (b=1;b<=a;b++)
	{
	d = Request.Cookies(b).Count
	if (d)
		{
		for (c=1;c<=d;c++) 
			{ 
			Response.Write(Request.Cookies(b)(c) + " ") 
			}
		Response.Write("<BR>\r")
		}
	else
		{ 
		Response.Write(Request.Cookies(b) + "<BR>\r")
		}
	}
%> 
</HTML>

      

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2967.html