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

Common pitfalls of using React Hooks

React Hooks is a new feature introduced in React ...

Solution to the problem "Table mysql.plugin doesn't exist" when deploying MySQL

Today I deployed the free-installation version of...

Videojs+swiper realizes Taobao product details carousel

This article shares the specific code of videojs+...

How to submit the value of a disabled form field in a form Example code

If a form field in a form is set to disabled, the ...

Should I use UTF-8 or GB2312 encoding when building a website?

Often when we open foreign websites, garbled char...

The whole process of configuring hive metadata to MySQL

In the hive installation directory, enter the con...

Six border transition effects implemented by CSS3

Six effectsImplementation Code html <h1>CSS...

The latest Linux installation process of tomcat8

Download https://tomcat.apache.org/download-80.cg...

How to backup and restore the mysql database if it is too large

Command: mysqlhotcopy This command will lock the ...

Use thead, tfoot, and tbody to create a table

Some people use these three tags in a perverted wa...

How to set focus on HTML elements

Copy code The code is as follows: <body <fo...

Simple example of using Docker container

Table of contents 1. Pull the image 2. Run the im...