获取cookie中的某个参数值

因为cookie的值是很多key=value连接起来的字符串,所以如果要取cookie中某个key的值:

function getCookie(name) {
    let cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        let cookies = document.cookie.split(';');
        for (let i = 0; i < cookies.length; i++) {
            let cookie = cookies[i].trim();
            // 判断这个cookie的参数名是不是我们想要的
            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}

  

转载于:https://www.cnblogs.com/jzss/p/10838985.html

你可能感兴趣的:(获取cookie中的某个参数值)