js uses cookies to remember user page operations

js uses cookies to remember user page operations

Preface

During the development process, we sometimes encounter similar requirements, such as remembering the operations performed by users at the browser level. I have made a function before, which used a drag plug-in to display a report chart similar to a nine-square grid. Each graph can be displayed and hidden. If the user clicks the show or hide button, the browser will retain the last operation result when entering the system next time. The core part is to use js to operate cookies, and the specific business part is to trigger a click event on a graphic. If it is hidden, the div corresponding to the graphic will be deleted from the cookie. When it is displayed, the graphic div will be written into the cookie. This article only records some cookie operations. You can write specific business codes according to your actual situation.

When are cookies used?

  • The cookie has a size of 4kb, and an empty string will be returned if the length exceeds;
  • Cookies are stored on the client and can be easily modified and viewed, so cookies cannot be used to store important information;
  • The cookie life cycle ends after the browser is closed. If you want to use it within a certain period of time, you can set the validity period for the cookie.

Cookie, sometimes also used in its plural form Cookies, refers to data (usually encrypted) stored on the user's local terminal by certain websites in order to identify the user and conduct session tracking. The specifications defined in RFC2109 and 2965 are now obsolete, and the latest superseding specification is RFC6265 [1]. (It can be called browser cache)
——Quoted from Baidu Encyclopedia

Here’s how

1. Set cookie parameters: cname: cookie name, cvalue: cookie value, exdays: cookie expiration time

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;
}

2. Get cookies

function getCookie(cname)
{
 var name = cname + "=";
 var ca = document.cookie.split(';');
 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 "";
}

3. Deleting cookies

function delCookie(cname)
{
 var exp = new Date(); 
 exp.setTime(exp.getTime()-1); 
 var cval = getCookie(cname);
 if(cval != null){
	document.cookie = cname + "=" + cval + ";expires=" + exp.toGMTString();
 }
}

I hope this article is helpful to you. If it is useful, remember to follow me and continue to output more content for you later.

Reference: Cookie Operation

Summarize

This is the end of this article about how js uses cookies to remember user page operations. For more relevant js cookies to remember users content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • js reads and writes COOKIE to implement code to remember account or password (js reads and writes COOKIE)
  • JavaScript uses cookies to remember account and password
  • Using cookies in js to remember passwords
  • js cookie to realize the function of remembering password
  • Static pages use JS to read cookies to remember user information
  • js uses cookies to remember the returned page usage example
  • Example of using cookie to remember username in js
  • How to use cookies in JavaScript to remember passwords and introduce cookie-related functions

<<:  How to create your own image using Dockerfile

>>:  MYSQL A question about using character functions to filter data

Recommend

idea combines docker to realize image packaging and one-click deployment

1. Install Docker on the server yum install docke...

HTTPS Principles Explained

As the cost of building HTTPS websites decreases,...

Detailed explanation of basic interaction of javascript

Table of contents 1. How to obtain elements Get i...

Detailed analysis of each stage of nginx's http request processing

When writing the HTTP module of nginx, it is nece...

How to configure MySQL master-slave synchronization in Ubuntu 16.04

Preparation 1. The master and slave database vers...

Specific use of Linux man command

01. Command Overview Linux provides a rich help m...

Build Tomcat9 cluster through Nginx and realize session sharing

Use Nginx to build Tomcat9 cluster and Redis to r...

Analysis of the principle of Rabbitmq heartbea heartbeat detection mechanism

Preface When using RabbitMQ, if there is no traff...

Vue detailed explanation of mixins usage

Table of contents Preface 1. What are Mixins? 2. ...

Specific use of node.js global variables

Global Object All modules can be called global: r...

Vue3+TypeScript implements a complete example of a recursive menu component

Table of contents Preface need accomplish First R...

Vue implements countdown between specified dates

This article example shares the specific code of ...

WeChat applet to save albums and pictures to albums

I am currently developing a video and tool app, s...