jQuery manipulates cookies

jQuery manipulates cookies

Copy code
The code is as follows:
jQuery.cookie = function(name, value, options) { if (typeof value != 'undefined') { // name and value given, set cookie options = options || {}; if (value === null) { value = ''; options.expires = -1; } var expires = ''; if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { var date; if (typeof options.expires == 'number') { date = new Date(); date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); } else { date = options.expires; } expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE } var path = options.path ? '; path=' + options.path : ''; var domain = options.domain ? '; domain=' + options.domain : ''; var secure = options.secure ? '; secure' : ''; document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); } else { // only name given, get cookie var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } }; function getcookie(name) { var cookie_start = document.cookie.indexOf(name); var cookie_end = document.cookie.indexOf(";", cookie_start); return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length))); } function setcookie(cookieName, cookieValue, seconds, path, domain, secure) { var expires = new Date(); expires.setTime(expires.getTime() + seconds); document.cookie = escape(cookieName) + '=' + escape(cookieValue) + (expires ? '; expires=' + expires.toGMTString() : '') + (path ? '; path=' + path : '/') + (domain ? '; domain=' + domain : '') + (secure ? '; secure' : ''); }
Directions: Provides convenient methods to operate cookies:
Copy code
The code is as follows:
$.cookie('the_cookie'); // Get the cookie $.cookie('the_cookie', 'the_value'); // Set the cookie $.cookie('the_cookie', 'the_value', { expires: 7 }); //Set a cookie with a time limit of 7 days$.cookie('the_cookie', '', { expires: -1 }); //Delete$.cookie('the_cookie', null); //Delete cookie Set the cookie name-value pair, validity period, path, domain, and security $.cookie('name', 'value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});

<<:  Introduction to TypeScript basic types

>>:  Using HTML to implement a voting website cheating scheme that restricts IP

Recommend

Summary of how to use bootstrap Table

This article shares with you how to use bootstrap...

Vue implements file upload and download

This article example shares the specific code of ...

Detailed explanation of the functions of each port of Tomcat

From the tomcat configuration file, we can see th...

Mysql 8.0.18 hash join test (recommended)

Hash Join Hash Join does not require any indexes ...

How to draw a mind map in a mini program

Table of contents What is a mind map? How to draw...

How to use translate and transition in CSS3

I always feel that translate and transition are v...

How to use Vue3 to achieve a magnifying glass effect example

Table of contents Preface 1. The significance of ...

In-depth analysis of the slow query problem of MySQL Sending data

Through an example, I shared with you the solutio...

How to use Linux to calculate the disk space occupied by timed files

Open the scheduled task editor. Cent uses vim to ...

Explanation of the usage scenarios of sql and various nosql databases

SQL is the main trunk. Why do I understand it thi...

js to achieve a simple lottery function

This article shares the specific code of js to im...

mysql calculation function details

Table of contents 2. Field concatenation 2. Give ...

Several ways to implement CSS height changing with width ratio

[Solution 1: padding implementation] principle: I...