Example:
<! DOCTYPE html>"en">"UTF-8"> <title>cookie</title> <script>function Setcookie (name,value,expires,path,domain,secure) {varCookiename=encodeuricomponent (name) +'='+encodeURIComponent (value); if(expires instanceof Date) {cookiename+='; expires='+expires; } if(path) {CookieName+=';p ath='+path; } if(domain) {domain cookiename+=';d omain='+domain; } if(Secure) {CookieName+='; secure'; } document.cookie=CookieName; } function GetCookie (name) {varCookiename=encodeuricomponent (name) +'='; varCookiestart =document.cookie.indexOf (cookiename); if(cookiestart>-1) { varCookieend=document.cookie.indexof (';', Cookiestart); if(cookieend==-1) {Cookieend=documet.cookie.length; } cookievalue=decodeuricomponent (Document.cookie.substring (cookiestart+cookiename.length,cookieend)); returnCookievalue; }} alert (GetCookie ('User')); Alert (GetCookie ('Email')); Alert (GetCookie ('URL')); function Setcookiedate (day) {//pass a number of days, such as pass 7, expire after 7 days varDate =NULL; if(typeofDay = =' Number'&& Day >0) {Date=NewDate (); Date.setdate (Date.getdate ()+Day ); } Else { Throw NewError ('the number of days you pass is illegal! Must be a number and greater than 0'); } returndate; }</script>Effect:
Set cookies
Firefox Settings privacy option We can see the cookie, we can see the result in localhost.
The user in the cookie
the URL in the cookie
Email in a cookie
Obtaining cookies
16:45:06 2017-09-04
JavaScript setting cookies and obtaining cookies