#In front-end web development, you can use JavaScript to set cookies on the client side. Specifically, cookies can be set through the document.cookie attribute.
The document.cookie attribute is a string that stores all the cookies of the current page. To set a new cookie, concatenate the name, value, and other optional attributes into a string and assign it to document.cookie. For example:
document.cookie = "name=value; expires=Fri, 31 Dec 2023 23:59:59 GMT; path=/";
The above code sets a Cookie named "name" with a value of "value". The expiration time is also specified as the end of 2023, and the path is set to the root path "/".
Note:
- Use semicolons and spaces to separate each attribute.
- Cookie values can contain any characters, but need to be URL encoded to prevent special characters from causing problems.
- Optional attributes include expires (expiration time), path (path), domain (domain name), secure (whether it is transmitted only through HTTPS connections), etc.
Please note that since cookies are information stored on the client side, sensitive information (such as passwords) should not be stored in cookies to avoid security risks.