javascript asp教程第9課--cookies
5989 ワード
Resonse Cookies in General:
We'll start with the Response Cookies collection.I don't think it could be any ease ier.You simply put the name of the cookie in the argment.The cores ponding value is a string.The only time it gets coplicatment
Click Here to run the script in a new window.
Setting a cookie with ASP is pretty simple.The format is Reponse.Cookie(「name」)=「value」.That「value」can be either a JavaScript string or an ASP native type such Request.Form(userEmail.)
Resonse Cookie Keys:
If on the first page of your ASP appection Resonse.Cookie is set,and subsequentlyon page two of your appication Resonse.Cookie(「myOnlyCookie」)is reassined a second valunce,The vance。
The solution is to eigther use multile cookies or to use multiple Keys in the SAME cookie.
Request Cookies:
Generaallyyou will find ASP cookie management to be far than Cient Side JavaScript cookies.Down below is the script the cookies.
Do I even need to explayin「first Cookie」and「secondCookie」?It's so easury.However,I will have to have explane the retrieval of Keys in“thirdCookie”.
We retrieve cookies in almost exactly the same way that we set them,and that gos for Keys well.If you keep troc of your Key names,then retrieving their values pretty.
To delete a cookie、give it an expiration date in the past.The follwing is not in our examples、but it would work.
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 VScript 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/inloop.Thantive 24
Click Here to run the script in a new window.
We do the same job three times.You decide for yourself which one like best.In the first example you can plinly see from the script,I ask a VScript to find and break down all the cookies.put。
What can I sayDon't lose track of your cookies and don't lose track of your Keys.Otherwise you might have to get a VBScript slam.
The new Eumerator()is okay.I use Enumerator two different ways;one way fails to capture the Key names,and the other way success fully 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 manippulate the cookie values inside JavaScript functions,then you will need new String()
We'll start with the Response Cookies collection.I don't think it could be any ease ier.You simply put the name of the cookie in the argment.The cores ponding value is a string.The only time it gets coplicatment
Click Here to run the script in a new window.
Setting a cookie with ASP is pretty simple.The format is Reponse.Cookie(「name」)=「value」.That「value」can be either a JavaScript string or an ASP native type such Request.Form(userEmail.)
Resonse Cookie Keys:
If on the first page of your ASP appection Resonse.Cookie is set,and subsequentlyon page two of your appication Resonse.Cookie(「myOnlyCookie」)is reassined a second valunce,The vance。
The solution is to eigther use multile cookies or to use multiple Keys in the SAME cookie.
<%@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>
The Setting of one or more Keys is pretty simple.It follows this format:Resonse.cookis("name")="value".Again,the"value"can ether be a JavaScrippt stript string or ASP native type.Thevantage。Request Cookies:
Generaallyyou will find ASP cookie management to be far than Cient Side JavaScript cookies.Down below is the script the cookies.
Response.Cookies("thirdCookie")("firstKey")="Here's the first Key."
Response.Cookies("thirdCookie")("secondKey")="Here's the second Key."
Click Here to run the script in a new window.Do I even need to explayin「first Cookie」and「secondCookie」?It's so easury.However,I will have to have explane the retrieval of Keys in“thirdCookie”.
We retrieve cookies in almost exactly the same way that we set them,and that gos for Keys well.If you keep troc of your Key names,then retrieving their values pretty.
<%@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>
Deleting Cookies:To delete a cookie、give it an expiration date in the past.The follwing is not in our examples、but it would work.
var thirdCookieFirstKey = Request.Cookies("thirdCookie")("firstKey")
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 VScript 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/inloop.Thantive 24
Click Here to run the script in a new window.
We do the same job three times.You decide for yourself which one like best.In the first example you can plinly see from the script,I ask a VScript to find and break down all the cookies.put。
What can I sayDon't lose track of your cookies and don't lose track of your Keys.Otherwise you might have to get a VBScript slam.
The new Eumerator()is okay.I use Enumerator two different ways;one way fails to capture the Key names,and the other way success fully 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 manippulate the cookie values inside JavaScript functions,then you will need new String()
var Yesterday=new Date()
Yesterday.setDate(Yesterday.getDate() - 1)
myExpire = (Yesterday.getMonth() + 1) + "/" + Yesterday.getDate()
myExpire += "/" + Yesterday.getFullYear()
Response.Cookies("firstCookie").Expires=myExpire
Also,if you were to use Cient Side scripting to locate cookies,you would have noticed that there is an extra cookie.ASP keeps trace of its sessions using a cookie.