Example of how to set and obtain cookies in javascript: javascriptcookie
This example describes how to set and obtain cookies in javascript. We will share this with you for your reference. The details are as follows:
1. Set cookie
Function setCookie (cookieName, cookieValue, cookieExpires, cookiePath) {cookieValue = escape (cookieValue); // code latin-1 if (cookieExpires = "") {var nowDate = new Date (); nowDate. setMonth (nowDate. getMonth () + 6); cookieExpires = nowDate. toGMTString ();} if (cookiePath! = "") {CookiePath = "; Path =" + cookiePath;} document. cookie = cookieName + "=" + cookieValue + "; expires =" + cookieExpires + cookiePath ;}
2. Get cookie
Function getCookieValue (cookieName) {var cookieValue = document. cookie; var cookieStartAt = cookieValue. indexOf ("" + cookieName + "="); if (cookieStartAt =-1) {cookieStartAt = cookieValue. indexOf (cookieName + "=");} if (cookieStartAt =-1) {cookieValue = null;} else {cookieStartAt = cookieValue. indexOf ("=", cookieStartAt) + 1; cookieEndAt = cookieValue. indexOf (";", cookieStartAt); if (cookieEndAt =-1) {cookieEndAt = cookieValue. length;} cookieValue = unescape (cookieValue. substring (cookieStartAt, cookieEndAt); // decodes latin-1} return cookieValue ;}
Example:
<! Doctype html>
Note:
Google Chrome only supports online-cookies for security purposes, so it is ineffective during local tests. You need to upload the files to the server for a try.