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

Detailed explanation of the installation and use of Vue-Router

Table of contents Install Basic configuration of ...

js to achieve simple magnifying glass effects

This article example shares the specific code of ...

Example code of the spread operator and its application in JavaScript

The spread operator allows an expression to be ex...

CSS to achieve fast and cool shaking animation effect

1. Introduction to Animate.css Animate.css is a r...

Detailed explanation of how to use Teleport, a built-in component of Vue3

Table of contents 1. Teleport usage 2. Complete t...

MySQL Series 11 Logging

Tutorial Series MySQL series: Basic concepts of M...

A brief discussion on the perfect adaptation solution for Vue mobile terminal

Preface: Based on a recent medical mobile project...

MySQL data insertion efficiency comparison

When inserting data, I found that I had never con...

Detailed Introduction to MySQL Innodb Index Mechanism

1. What is an index? An index is a data structure...

Two ways to specify the character set of the html page

1. Two ways to specify the character set of the h...

What to do if the container started by docker run hangs and loses data

Scenario Description In a certain system, the fun...