Problem:
This allows you to use the previous settings when accessing the page or share data between different pages. For example, if you set the page font size when visiting a website, you will want to use the same settings for browsing next time without repeated settings.
Solution:
When you browse the page and make settings, save these settings in the cookie and read the settings in the cookie for the next visit.
Refer to the following script:
Copy codeThe Code is as follows:
// Utility function to retrieve an expiration data in proper format;
Function getExpDate (days, hours, minutes)
{
Var expDate = new Date ();
If (typeof (days) = "number" & typeof (hours) = "number" & typeof (hours) = "number ")
{
ExpDate. setDate (expDate. getDate () + parseInt (days ));
ExpDate. setHours (expDate. getHours () + parseInt (hours ));
ExpDate. setMinutes (expDate. getMinutes () + parseInt (minutes ));
Return expDate. toGMTString ();
}
}
// Utility function called by getCookie ()
Function getCookieVal (offset)
{
Var endstr = document. cookie. indexOf (";", offset );
If (endstr =-1)
{
Endstr = document. cookie. length;
}
Return unescape (document. cookie. substring (offset, endstr ));
}
// Primary function to retrieve cookie by name
Function getCookie (name)
{
Var arg = name + "= ";
Var alen = arg. length;
Var clen = document. cookie. length;
Var I = 0;
While (I <clen)
{
Var j = I + alen;
If (document. cookie. substring (I, j) = arg)
{
Return getCookieVal (j );
}
I = document. cookie. indexOf ("", I) + 1;
If (I = 0) break;
}
Return;
}
// Store cookie value with optional details as needed
Function setCookie (name, value, expires, path, domain, secure)
{
Document. cookie = name + "=" + escape (value) +
(Expires )? "; Expires =" + expires: "") +
(Path )? "; Path =" + path: "") +
(Domain )? "; Domain =" + domain: "") +
(Secure )? "; Secure ":"");
}
// Remove the cookie by setting outgoing ent expiration date
Function deleteCookie (name, path, domain)
{
If (getCookie (name ))
{
Document. cookie = name + "=" +
(Path )? "; Path =" + path: "") +
(Domain )? "; Domain =" + domain: "") +
"; Expires = Thu, 01-Jan-70 00:00:01 GMT ";
}
}
Use the getCookie (name) function to read the value saved in the cookie. The parameter name is the name of the cookie item. If the cookie does not exist, an empty string is returned.
Use the setCookie () function to save the value of the cookie item. The first and second parameters are the name and value of the cookie item respectively. If you want to set an expiration time for it, you need to set the third parameter. Here, you need to get a correct format parameter through getExpDate.
Finally, you can use deleteCookie () to delete an existing cookie by actually making it expire.
Cookie stores data on the client. The page script can only read the cookie values of the domain and server. If there are multiple servers in the domain, you need to set the fifth parameter to specify the server. The browser capacity is generally limited to 20 name/value pairs per server. Each cookie item cannot exceed 4000 characters. More realistically, a single cookie item should be less than 2000 characters, in other words, do not use cookies to store large volumes of data on the client.
Different browsers use different methods to save cookies. IE creates a text file for the cookies in each domain, while Netscape stores all cookies in the same text file.
Note: The cookie is stored on the client, so it is affected by browser settings. For example, the user may disable the cookie. To check whether the browser supports cookies, use the property navigator. cookieEnabled to determine whether the browser supports cookies.