龙空技术网

js操作获取和设置cookie

尚硅谷教育 223

前言:

今天朋友们对“js获取value”大体比较关心,我们都想要剖析一些“js获取value”的相关资讯。那么小编也在网摘上收集了一些有关“js获取value””的相关文章,希望咱们能喜欢,朋友们快快来学习一下吧!

web前端核心技术如何使用js操作获取和设置cookie

//创建cookie

function setCookie(name, value, expires, path, domain, secure) {

var cookieText = encodeURIComponent(name) + ‘=’ + encodeURIComponent(value);

if (expires instanceof Date) {

cookieText += ‘; expires=’ + expires;

}

if (path) {

cookieText += ‘; expires=’ + expires;

}

if (domain) {

cookieText += ‘; domain=’ + domain;

}

if (secure) {

cookieText += ‘; secure’;

}

document.cookie = cookieText;

}

//获取cookie

function getCookie(name) {

var cookieName = encodeURIComponent(name) + ‘=’;

var cookieStart = document.cookie.indexOf(cookieName);

var cookieValue = null;

if (cookieStart > -1) {

var cookieEnd = document.cookie.indexOf(‘;’, cookieStart);

if (cookieEnd == -1) {

cookieEnd = document.cookie.length;

}

标签: #js获取value