Use JavaScript to operate on cookies

xiaoxiao2021-03-06  14

Cookie is a small number of named data stored in a web browser, which is associated with a particular web page or website.

Cookie is a string property. In addition to the names and values, each cookie has four optional properties. Expires Specifies the living cycle of the cookie. By default, cookies are temporarily present, and its stored values ​​exist only during the browser session. This value disappears when the user exits the browser.

Path Specifies the web page associated with cookies. By default, cookies will be associated with the web page that creates its web pages and the web pages in the same directory in the same directory. Sometimes you may want to make cookies to see the entire website, regardless of which web page creation is created, you need to specify the PATH property. Then, the web page from the same network server, as long as its URL contains the specified path, you can share this cookie.

Domain specifies the domain that is associated with cookies. By default, only and creating a cookie page from the page of the same web server to access this cookie, the value of Domain is the host name of the server where the cookie's web is located. If you want to make the cookie visible to multiple servers in the same domain, you need to set the Domain property.

Secure specifies how to transfer a cookie value on the network, which is a Boolean value. By default, cookies are unsafe.

Below is a basic code for cookie operations for reference:

Function SetCookie (Cookiename, CookieValue, Expires, Path, Domain, Secure) {

Document.cookie =

Escape (cookiename) '=' escape (cookievalue)

(Expires? '; expires =' expires.togmtstring (): '')

(Path? '; Path =' PATH: '')

(Domain? '; domain =' domain: '')

(Secure? '; secure': '');

}

Function getCookie (cookiename) {var arycookie = document.cookie.split (";"); for (var i = 0; i

}

转载请注明原文地址:https://www.9cbs.com/read-49805.html

New Post(0)