Home > Web Front-end > JS Tutorial > What are the cookie attributes provided by JavaScript?

What are the cookie attributes provided by JavaScript?

青灯夜游
Release: 2018-12-15 09:41:13
Original
2370 people have browsed it

The cookie attributes provided by JavaScript include: expires attribute (declaring the expiration time of the cookie), max-age attribute (declaring the maximum time of cookie activity), domain attribute (defining a valid domain name), and path attribute.

What are the cookie attributes provided by JavaScript?

What is Cookie?

Cookies are a large amount of information that persists between the server and the client. The web browser will store this information while browsing.

Cookies usually contain information as a string in the form of a semicolon-separated name-value pair; it maintains the user's status and remembers user information in web pages.

How to create cookies in JavaScript?

In JavaScript, we can create, read, update and delete cookies using the document.cookie property.

Basic syntax for creating cookies:

document.cookie = “name = value” ;
Copy after login

JavaScript's Cookie attributes

JavaScript provides some optional attributes to enhance The functions of cookies, let’s take a look at these attributes:

expires attribute:

can maintain the status of the cookie until the specified date and time; that is, it Declaring a date and time that represents the duration of the cookie's activity is one of the ways to create a persistent cookie.

Example: Set the validity period of the cookie for "username = php Chinese website" to 2030, August 20, 12:00:00

document.cookie = “username = php中文网; expires = Sun,20 Aug 2030 12:00:00 UTC” ;
Copy after login

Note: expires attribute, once declared If the time is in the past (any time in the past will do), the cookie will be automatically deleted; it is also a way to delete cookies.

Example: Set the validity period to 1970, January 1, 00:00:00

document.cookie = "username=php中文网; expires= Thu, 01 Jan 1970 00:00:00 GMT";
Copy after login

max-age attribute

The status of the cookie can be maintained for a specified time, which is calculated in seconds. It is also one of the ways to create persistent cookies.

document.cookie = "username =php中文网; max-age ="  +(60 * 60 * 24 * 365)+";"
Copy after login

Note: Cookies can also be deleted using the max-age attribute. You only need to provide zero or a negative number (indicating the number of seconds) to the max-age attribute.

 document.cookie = "name = php中文网; max-age = 0" ;
Copy after login

domain attribute

It is used to specify the domain in which the cookie is valid; the cookie will be valid only in the specified domain name.

Suppose we provide any domain name for the domain attribute, for example:

domain = php .cn
Copy after login

Here, the cookie is valid for the given domain and all its subdomains.

But, if we provide any subdomain to the attribute, for example:

domain=img.php .cn
Copy after login

Here, the cookie will be valid only for the given subdomain. Therefore, it is a better method to provide the cookie with a domain name rather than a subdomain name to make the cookie valid under the specified domain name.

path attribute: Extend the scope of the cookie to all pages of the website

Simple example of JavaScript Cookie

Example: JavaScript creates Cookie, gets Cookie, reads Cookie

<script>
function setCookie(cname,cvalue,exdays){
	var d = new Date();
	d.setTime(d.getTime()+(exdays*24*60*60*1000));
	var expires = "expires="+d.toGMTString();
	document.cookie = cname+"="+cvalue+"; "+expires;
}
function getCookie(cname){
	var name = cname + "=";
	var ca = document.cookie.split(&#39;;&#39;);
	for(var i=0; i<ca.length; i++) {
		var c = ca[i].trim();
		if (c.indexOf(name)==0) { return c.substring(name.length,c.length); }
	}
	return "";
}
function checkCookie(){
	var user=getCookie("username");
	if (user!=""){
		alert("欢迎 " + user + " 再次访问");
	}
	else {
		user = prompt("请输入你的名字:","");
  		if (user!="" && user!=null){
    		setCookie("username",user,30);
    	}
	}
}
</script>
Copy after login

Rendering:

What are the cookie attributes provided by JavaScript?

When entering the name, press the "OK" button Afterwards, refresh the current page, and it will appear:

What are the cookie attributes provided by JavaScript?

##Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

The above is the detailed content of What are the cookie attributes provided by JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template