The examples in this article describe the use of JavaScript cookies. Share to everyone for your reference, specific as follows:
One, what is a cookie?
Cookies are pages used to hold information, such as automatic login, remembering user names, and so on.
Second, the characteristics of cookies
Share a cookie for all pages in the same Web site
Cookie has number, size limit
Cookie has expiration time
Third, how to use cookies?
To write cookies by Document.cookie
Open the browser to view the cookie, and you will find that the newly defined cookie does not overwrite the original.
If no expiration time is set, the cookie is emptied when the browser is closed. How do I set the expiration time? The answer is: expires. Generally we will use the Date object.
var d = new Date ();
D.settime (D.gettime () + 1 * 3600 * 1000);
Document.cookie = ' username=abc; Expires= ' + d.togmtstring ();
We can see through the Firefox browser that the username expiration time is 1 hours after the current time.
Finally, the method for obtaining cookies is encapsulated:
function Setcookie (name,value,hours) {
var d = new Date ();
D.settime (D.gettime () + hours * 3600 * 1000);
Document.cookie = name + ' = ' + value + '; Expires= ' + d.togmtstring ();
}
Learned how to set cookies, so how do you read cookies?
First, let's look at what type of content is in the cookie?
Document.cookie = ' username=abc ';
Document.cookie = ' password=123 ';
Document.cookie = ' email=abcdef@123.com ';
typeof Document.cookie; String
alert (document.cookie);//' username=abc; password=123; email=abcdef@123.com '
Get a string of strings that need to be noted, each; There is a space behind it.
So how do we get a specific number? Attached code:
function GetCookie (name) {
var arr = Document.cookie.split ('; ');
for (var i = 0; i < arr.length i++) {
var temp = arr[i].split (' = ');
if (temp[0] = = name) {return
temp[1];
}
}
return ';
}
In addition to setting up and getting cookies, we can also delete cookies. We often see the use of the Internet to clear the user name such a function, in fact, is used to clear cookies.
It's really easy to clear cookies, as long as the expiration time is over time.
function Removecookie (name) {
var d = new Date ();
D.settime (D.gettime ()-10000);
Document.cookie = name + ' = 1; Expires= ' + d.togmtstring ();
}
Finally, we'll package, get, and erase cookies into a cookie.js
function Setcookie (name,value,hours) {
var d = new Date ();
D.settime (D.gettime () + hours * 3600 * 1000);
Document.cookie = name + ' = ' + value + '; Expires= ' + d.togmtstring ();
}
function GetCookie (name) {
var arr = Document.cookie.split ('; ');
for (var i = 0; i < arr.length i++) {
var temp = arr[i].split (' = ');
if (temp[0] = = name) {return
temp[1];
}
}
return ';
}
function Removecookie (name) {
var d = new Date ();
D.settime (D.gettime ()-10000);
Document.cookie = name + ' = 1; Expires= ' + d.togmtstring ();
}
attached: Here's another basic application for cookies: JavaScript cookie record username
More readers interested in JavaScript-related content can view this site: "JavaScript in the JSON Operation tips Summary", "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills summary", " JavaScript animation effects and tips Summary, "JavaScript Error and debugging skills Summary", "JavaScript data structure and algorithm skills summary", "JavaScript traversal algorithm and Skills summary" and "JavaScript Mathematical operation Usage Summary"
I hope this article will help you with JavaScript programming.