javascript asp教程第九课--cookies
response cookies in general:
we'll start with the response cookies collection. i don't think it could be any easier. you simply put the name of the cookie in the argument. the corresponding value is a string. the only time it gets complicated is when you use keys (which i demonstrate below).
<%@language="javascript"%> <% var tomorrow=new date() tomorrow.setdate(tomorrow.getdate() + 1) myexpire = (tomorrow.getmonth() + 1) + "/" + tomorrow.getdate() myexpire += "/" + tomorrow.getfullyear() response.cookies("firstcookie") = "i like cookies." response.cookies("firstcookie").expires=myexpire response.cookies("secondcookie") = "asp cookies are easy." response.cookies("secondcookie").expires=myexpire response.cookies("thirdcookie")("firstkey")="here's the first key." response.cookies("thirdcookie")("secondkey")="here's the second key." response.cookies("thirdcookie").expires=myexpire %> <html> we're just setting <%=response.cookies.count%> cookies.<br> <a href="script09a.asp">click here</a> to retrieve these cookies. </html>
click here to run the script in a new window.
setting a cookie with asp is pretty simple. the format is
response cookie keys:
if on the first page of your asp application response.cookies("myonlycookie") is set, and subsequently on page two of your application response.cookies("myonlycookie") is reassigned a second value, then only the second value will remain. the first value is lost in this circumstance.
the solution is to either use multiple cookies or to use multiple keys in the same cookie.
response.cookies("thirdcookie")("firstkey")="here's the first key." response.cookies("thirdcookie")("secondkey")="here's the second key."
the setting of one or more keys is pretty simple. it follows this format:
request cookies:
generally you will find asp cookie management to be far easier than client side javascript cookies. down below is the script that retrieves the cookies.
<%@language="javascript"%> <% if (response.cookies.count <= 0) { response.redirect("script09.asp") } var firstcookie = request.cookies("firstcookie"); var secondcookie = request.cookies("secondcookie"); var thirdcookie2keys = request.cookies("thirdcookie")("firstkey") thirdcookie2keys += " " + request.cookies("thirdcookie")("secondkey"); %> <html> there are <%=request.cookies.count%> cookies.<br> 1) <%=firstcookie%><br> 2) <%=secondcookie%><br> 3) <%=thirdcookie2keys%><br> <a href="script09b.asp">click here</a> to see how we would sort cookies if we didn't know their names. </html>
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>
click here to run the script in a new window.
we do the same job three times. you decide for yourself which one you like best. in the first example you can plainly see from the script, i ask a vbscript function to find and break down all the cookies. i then output the data back to the waiting javascript variable.
var longcookie = foreachcookie()
what can i say? don't lose track of your cookies and don't lose track of your keys. otherwise you might have to get a vbscript slim jim.
the new enumerator() is okay. i use enumerator two different ways; one way fails to capture the key names, and the other way successfully captures the key names (but it needs unescape() to get the key values to print normally).
in round three, i use a pair of for loops, but they're not as functional as for/in would be. (notice the lack of key names.)
misc. notes:
we didn't use new string( ) in this lesson. but remember, if you want to manipulate the cookie values inside javascript functions or methods, then you will need new string().
var firstcookie = new string(request.cookies("firstcookie") ) firstcookie = firstcookie.touppercase()
also, if you were to use client side scripting to locate cookies, you would have noticed that there is an extra cookie. asp keeps track of its sessions using a cookie.