Home > front end >  Novice, write and read about the cookie case problems, seek solutions
Novice, write and read about the cookie case problems, seek solutions

Time:11-04

The teacher let us according to the topic to write code, write a failed to come out in the afternoon, a few students discuss the along while also didn't figure out, so I want to ask how to write?


Cookies. Js code:

The function createCookie (name, value, days, path, domain, secure) {
If (days) {
Var date=new date ();
Date. SetTime (date. GetTime () + (days * 24 * 60 * 60 * 1000));
Var expires=date. ToUTCString ();
} the else var expires="";
CookieString=name + "=" + escape (value);
If (expires) cookieString +="; Expires="+ expires;
If (path) cookieString +="; Path="+ escape (path);
If (domain) cookieString +="; Domain="+ escape (domain);
If (secure) cookieString +="; Secure ";
Document. The cookie=cookieString;
}

The function getCookie (name) {
Var nameEquals=name + "=";
Var crumbs=document. Cookies. Split ('; ');

for (var i=0; iVar crumb=crumbs [I] the trim ();
If (crumb. IndexOf (nameEquals)==0) {
Return unescape (crumb. Substring (nameEquals. Length, crumb. Length));
}
}
return null;
}

The function deleteCookie (name) {
CreateCookie (name, "", 1);
}


Cookietest. HTML:


<meta charset="UTF-8">
Cookie Testing
<script SRC="https://bbs.csdn.net/topics/cookies.js" & gt; </script>
<script>
Window. The onl oad=function () {
Var cookievalue=https://bbs.csdn.net/topics/prompt (" the Cookie Value: ");
CreateCookie (" myCookieData cookievalue);
}
</script>

<body>





Cookietest2 code:


<meta charset="UTF-8">
Cookie Testing
<script SRC="https://bbs.csdn.net/topics/cookies.js" & gt; </script>
<script>
Window. The onl oad=function () {
Document. The getElementById (" output "). The innerHTML="Your cookie value:" + getCookie (" myCookieData ");
Document. The getElementById (" delete "). The onclick=deleteCookie ();
}

</script>

<body>




CodePudding user response:

Also in the morning doing tasks admire you
CreateCookie (name, value, days, path, domain, secure) how so many parameters can now add a days time according to the questions I understand the meaning of is cookietest HTML page responsible for store cookies and stored in a cookie use # as a delimiter cookietest. HTML is responsible for deleting and show the cookie when branch show feeling is this

CodePudding user response:

I'm not path and domain not considered

CodePudding user response:

reference 1/f, Mr Skinny reply:
is in the morning doing tasks admire you
CreateCookie (name, value, days, path, domain, secure) how so many parameters can now add a days time according to the questions I understand the meaning of is cookietest HTML page responsible for store cookies and stored in a cookie use # as a delimiter cookietest. HTML is responsible for deleting and show the cookie branch show when feelings are so

Because it is a case study, the other parameters are used to learn to understand, then the topic is to understand, but use very fan??

CodePudding user response:

Have bosses can see no... ?

CodePudding user response:

 
The function createCookie ({name, value, days, path, domain, secure}) {

}


Such a custom function would be much better