


JavaScript implements cookie-based storage class instance_javascript skills
The example in this article describes the implementation of Cookie-based storage class in JavaScript. Share it with everyone for your reference. The specific analysis is as follows:
Through this JS class, you can use cookies just like using sessions, it’s very simple!
/* * CookieStorage.js * 本类实现像localStorage和sessionStorage一样的存储API * 不同的是,它是基于HTTP Cookies实现的. */ function CookieStorage(maxage, path) { // 两个参数分别代表储存有效期和作用域 // 获取一个储存全部cookies的对象 var cookies = (function() { // 类型之前介绍的getCookies函数 var cookies = {}; // 该对象最终会返回 var all = document.cookie; // 以大字符串的形式获取所有cookies的信息 if (all === "") // 如果该属性为空白符 return cookies; // 返回一个空对象 var list = all.split("; "); // 分离出名/值对 for(var i = 0; i < list.length; i++) { // 遍历每个cookie var cookie = list[i]; var p = cookie.indexOf("="); // 找到第一个“=”符号 var name = cookie.substring(0,p); // 获取cookie的名字 var value = cookie.substring(p+1); // 获取cookie对应的值 value = decodeURIComponent(value); // 对其值进行解码 cookies[name] = value; // 将名值对存储到对象中 } return cookies; }()); // 将所有cookie的名字存储到一个数组中 var keys = []; for(var key in cookies) keys.push(key); // 现在定义储存API公共的属性和方法 // 储存的cookies的个数 this.length = keys.length; // 返回第n个cookie的名字,如果n越界则返回null this.key = function(n) { if (n < 0 || n >= keys.length) return null; return keys[n]; }; // 返回指定名字的cookie值,如果不存在则返回null this.getItem = function(name){ return cookies[name] || null; }; // 储存cookie值 this.setItem = function(key, value) { if (!(key in cookies)) { // 如果要促成的cookie还不存在 keys.push(key); // 将指定的名字加入到储存所有cookie名的数组中 this.length++; // cookies个数加一 } // 将该名/值对数据存储到cookie对象中. cookies[key] = value; // 开始正式设置cookie. // 首先将要储存的cookie的值进行编码 // 同时创建一个“名称=编码后的值”形式的字符串 var cookie = key + "=" + encodeURIComponent(value); // 将cookie的属性也加入到该字符串中 if (maxage) cookie += "; max-age=" + maxage; if (path) cookie += "; path=" + path; // 通过document.cookie属性来设置cookie document.cookie = cookie; }; // 删除指定的cookie this.removeItem = function(key) { if (!(key in cookies)) return; // 如果cookie不存在,则什么也不做 // 从内部维护的cookies组删除指定的cookie delete cookies[key]; // 同时将cookie中的名字也在内部的数组中删除. // 如果使用ES5定义的数组indexOf()方法会更加简单. for(var i = 0; i < keys.length; i++) { // 遍历所有的名字 if (keys[i] === key) { // 当我们找到了要找的那个 keys.splice(i,1); // 将它从数组中删除. break; } } this.length--; // cookies个数减一 // 最终通过将该cookie的值设置为空字符串 //以及将有效期设置为0来删除指定的cookie. document.cookie = key + "=; max-age=0"; }; // 删除所有的cookies this.clear = function() { // 循环所有的cookies的名字,并将cookies删除 for(var i = 0; i < keys.length; i++) document.cookie = keys[i] + "=; max-age=0"; // 重置所有的内部状态 cookies = {}; keys = []; this.length = 0; }; }
I hope this article will be helpful to everyone’s JavaScript programming design.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



This website reported on March 7 that Dr. Zhou Yuefeng, President of Huawei's Data Storage Product Line, recently attended the MWC2024 conference and specifically demonstrated the new generation OceanStorArctic magnetoelectric storage solution designed for warm data (WarmData) and cold data (ColdData). Zhou Yuefeng, President of Huawei's data storage product line, released a series of innovative solutions. Image source: Huawei's official press release attached to this site is as follows: The cost of this solution is 20% lower than that of magnetic tape, and its power consumption is 90% lower than that of hard disks. According to foreign technology media blocksandfiles, a Huawei spokesperson also revealed information about the magnetoelectric storage solution: Huawei's magnetoelectronic disk (MED) is a major innovation in magnetic storage media. First generation ME

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Git is a fast, reliable, and adaptable distributed version control system. It is designed to support distributed, non-linear workflows, making it ideal for software development teams of all sizes. Each Git working directory is an independent repository with a complete history of all changes and the ability to track versions even without network access or a central server. GitHub is a Git repository hosted on the cloud that provides all the features of distributed revision control. GitHub is a Git repository hosted on the cloud. Unlike Git which is a CLI tool, GitHub has a web-based graphical user interface. It is used for version control, which involves collaborating with other developers and tracking changes to scripts and

How to correctly use sessionStorage to store sensitive information requires specific code examples. Whether in web development or mobile application development, we often need to store and process sensitive information, such as user login credentials, ID numbers, etc. In front-end development, using sessionStorage is a common storage solution. However, since sessionStorage is browser-based storage, some security issues need to be paid attention to to ensure that the stored sensitive information is not maliciously accessed and used.

Common problems and solutions for cookie settings, specific code examples are required. With the development of the Internet, cookies, as one of the most common conventional technologies, have been widely used in websites and applications. Cookie, simply put, is a data file stored on the user's computer that can be used to store the user's information on the website, including login name, shopping cart contents, website preferences, etc. Cookies are an essential tool for developers, but at the same time, cookie settings are often encountered

In our daily use of computers and the Internet, we are often exposed to cookies. A cookie is a small text file that saves records of our visits to the website, preferences and other information. This information may be used by the website to better serve us. But sometimes, we need to find cookie information to find the content we want. So how do we find cookies in the browser? First, we need to understand where the cookie exists. in browser

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
