JavaScript ASP Tutorial Nineth Lesson--cookies_asp Basics

Source: Internet
Author: User

Response Cookies in general:

We ' ll start with the Response Cookies collection. I don ' t do it could be any easier. You are simply put the name of the cookies in the argument. The corresponding value is a string. The only time it gets complicated are when your 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 ()

Res Ponse. 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" ".
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 the ASP is pretty simple. The format is response.cookies ("name") = "value" . That "value" can is either a JavaScript string or an ASP native type such as Request.Form ("UserEmail").

Response Cookie Keys:

If on the 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 would remain. The the ' The ' 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" ".
Response.Cookies ("Thirdcookie") ("secondkey") = "Here ' s the second Key."

The Setting of one or more Keys are pretty simple. It follows this format: response.cookies ("name") ("key") = "value" . Again, the "value" can either be a JavaScript string or ASP native type. The advantage of using keys is this you can store multiple key/value pairs inside the very same cookie.

Request Cookies:

Generally you'll find the ASP cookie management to be far easier than Client Side JavaScript cookies. Down below are 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 [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'll have to have explain the retrieval of Keys in "Thirdcookie".

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

var Thirdcookiefirstkey = request.cookies ("Thirdcookie") ("Firstkey")

deleting Cookies:

To delete a cookie, give it a expiration date in the past. The following is isn't 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? All of the, don ' t does that. But Secondly, we have options.

We can use a VBScript Function with a For/each loop. We can use the Javascript ' s New Enumerator () or we can use a pair of the 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.co Okies if Request.Cookies (x). HasKeys then for all y in Request.Cookies (x) z = z & x & ":" & Y & "=" & Request.Cookies (x) (y) &A mp
		"; "
	Next Else Z = z & x & "=" & Request.Cookies (x) & ";" End If Next Foreachcookie = Z-end Function </SCRIPT> <% Response.Write ("<strong>let's" use a VBScript functi ' 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 A Lternate method of using Enumerator () Response.Write ("<br>\r<strong>or ... </STRONG>") Response.writ
		E (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") Re Sponse. Write ("<strong>let's use a pair's JavaScript loops to") Response.Write ("Sort out" Cookies and keys.</strong& Gt;<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 are decide for the yourself which one you like best. In the ' the ' I example you can plainly the ' script, I ask a VBScript function ' to find ' and ' break ' all 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, with the other way successfully the key captures (names it but needs () 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 is. (Notice the lack of Key names.)

Misc. Notes:

We didn ' t use the new String () in this lesson. But Remember, if you want to manipulate the cookie values inside JavaScript functions or methods, then your'll need new S Tring ().

var Firstcookie = new String (Request.Cookies ("Firstcookie"))
Firstcookie = Firstcookie.touppercase ()

Also, if you are were to use Client Side scripting to locate cookies, you would the have noticed that there are an extra cookie. ASP keeps track of its sessions using a cookie.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.