-
斯蒂芬大帝
以下是可用于创建和检索Cookie的函数。var createCookie = function(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
else {
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";}function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) {
c_end = document.cookie.length;
}
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";}
-
POPMUISE
简约和全功能的ES6方法:const setCookie = (name, value, days = 7, path = '/') => {
const expires = new Date(Date.now() + days * 864e5).toUTCString()
document.cookie = name + '=' + encodeURIComponent(value) + '; expires=' + expires + '; path=' + path}const getCookie = (name) => {
return document.cookie.split('; ').reduce((r, v) => {
const parts = v.split('=')
return parts[0] === name ? decodeURIComponent(parts[1]) : r }, '')}const deleteCookie = (name, path) => {
setCookie(name, '', -1, path)}
-
鸿蒙传说
Mozilla提供了一个简单的框架,用于读取和编写具有完全unicode支持的cookie以及如何使用它的示例。一旦包含在页面上,您就可以设置一个cookie:docCookies.setItem(name, value);读一个cookie:docCookies.getItem(name);或删除cookie:docCookies.removeItem(name);例如:// sets a cookie called 'myCookie' with value 'Chocolate Chip'docCookies.setItem('myCookie', 'Chocolate Chip');
// reads the value of a cookie called 'myCookie' and assigns to variablevar myCookie = docCookies.getItem('myCookie');
// removes the cookie called 'myCookie'docCookies.removeItem('myCookie');有关Mozilla的document.cookie页面的更多示例和详细信息,请参阅。