Loading

js 根据 key 获取 cookie 的值

/**
 * 获取指定的cookie
 * @param {String} key cookie的key
 * @returns {String}
 */
export function getCookie(key) {
  const cookie = document.cookie.split(';')
  for (let i = 0, len = cookie.length; i < len; i++) {
    const cur = cookie[i].split('=')
    if (key === cur[0]) {
      return cur[1]
    }
  }

  return ''
}

注:cookie属性有http-only的,js无法获取该值

posted @ 2021-08-03 09:58  Frank-Link  阅读(435)  评论(0编辑  收藏  举报