First, what is a cookie
Cookies are also called HTTP cookies, which are originally used for client and server-side sessions, because HTTP is a stateless protocol, in order to maintain the user and to track user information, so the introduction of cookies and Session,cookie is primarily stored in the client, Session is stored on the server side of the
Ii. Limitations of cookies
The number of cookies under each specific domain name is limited, Fixfox is 50, opera is 30, Chrome and Safari are unlimited, and each cookie has a maximum limit of 4K, if the content of the cookie exceeds 4K, Then the call will return an empty string.
It is important to note that, if the cookie is stored on a local disk, then chrome cannot be output in the console, this time the solution is to try a different browser
PS: Since HTTP will be attached every time you initiate a network request, for functionality and performance considerations, you should minimize the number of cookies used and use a relatively small cookie
Third, the basic use of cookies
Cookies have the concept of domain and path . Domain is the concept of domains, because browsers are a security-aware environment, so there is no mutual access to cookies between different domains (of course, through special settings to achieve cookie cross-domain access). The path is the concept of routing, a Web page created by the cookie can only be used in the same directory or subdirectory of the page to access all the pages, and can not be accessed by other directory pages
A cookie is a storage method consisting of key-value pairs in the approximate format of Name=value
Document.cookie=name=value; [Exprises=date]; [Path=path] [] for optional content
Here we introduce a small example to introduce, cookie hypothesis we want to make such a function, in the user login interface when the user's personal information stored in a cookie for later use, the relevant code is as follows:
Delete Cookies
There is no way to delete cookies directly in the cookie operation, but we can think in a different direction because Cokie has a valid time, so if we let the cookie expire in time, then this cookie is equivalent to not being effective. So the effect can be the same as being deleted (the default cookie does not set the period of time is expired when you close the browser)
We add a button and a Y trigger script according to the example above, as follows:
function Btn_deleteuser () { //Gets the value of the user input box var User=document.getelementbyid ("user"); var Input=user.value; Creates a time object and gets the current time var date=new date (); Set the time to the day before the current time date.setdate (Date.getdate ()-1); Convert time into GMT format var time=date.togmtstring (); Set Expiration time for field document.cookie= "uername=" +input+ "expires=" +time; }
The idea of this script is to implement the ability to delete a cookie.
Find cookies
Here to find out whether the value of a cookie exists, here we specifically to do is to determine whether the value of the cookie is empty, and then to determine whether the cookie you entered is a name exists, if there is a value to obtain its corresponding values, the specific code W3 school has been given, Specific as follows:
function GetCookie (c_name) {if (document.cookie.length>0) {//query whether the cookie is empty or empty, return "" C_start=document.cooki E.indexof (c_name + "=")//through the String object's IndexOf () to check whether the cookie exists, does not exist as-1 if (c_start!=-1) {C_start=c_start + C_name.length+1//The last +1 actually means "=", so we get the starting position of the cookie value c_end=document.cookie.indexof (";", C_start)//Actually I just saw index Of () the second parameter suddenly a little dizzy, and later remembered to indicate the location of the specified start index ... This sentence is to get the end position of the value. Because you need to consider whether it is the last item, so pass ";" C_end==-1 c_end=document.cookie.length return unescape (document.cookie.substring (c_start,c_e nd)//through substring () to get the value. Want to understand unescape () to know what escape () is to do, is a very important basis, want to know can be searched, at the end of the article will also explain the cookie code details}} return ""}
Deletion of all Cookies
To achieve the deletion of all cookies, according to my implementation of the idea is that the string cut through the method of each string, such as: Name=value, and then the string after each cut after the small string to add an expiration time, so that the cookie can be completely deleted .
Iv. Other uses of cookies
In addition to the Exprise attribute, this property is used to set the path to the cookie, which is accessed by default on the subdirectory of the current cookie page. However, by default we cannot access this cookie under any other parent directory, and we can do this by setting a cookie.
We assume that our files are stored in www.leslieyong.com/cn/dowload/index.html, so we can access cookies by default under the Dowload directory. At this point, we can set the Path property to achieve the effect that can be accessed under the Www.leslieyong.com root directory, as follows:
Document.cookie = "name=value;expires=date;path=/"
To implement this can be accessed under the CN directory it should be written like this
Document.cookie = "name=value;expires=date;path=/cn/"
As said above, we can achieve in the same domain between the value of the transfer, but in the cross-domain of how to implement the value, in fact, in addition to the cookie path, there is domain,domain this attribute can be implemented across domains, but must ensure that the two domain names have a public part, what is the public part? is like www.qq.com and www.sport.qq.com this has the same qq.com domain name, the specific use method is to set domain as the same part of the domain name, as follows:
Document.cookie = "name=value;expires=date;path=/;d omain=qq.com"
In addition, when using cookies, we also need to pay attention to the problem of encoding the cookie, because the cookie does not support commas, spaces, semicolons, so when setting up a cookie, you need to use Escape () to transcode the input information, Then use Unescape () at the time of the call to re-convert back, this input JavaScript basic knowledge of the scope, unclear please Baidu
Cookie Learning Guide