原生JS獲取cookie值數組
首先判斷cookie是否存在cookie
if (document.cookie && document.cookie != '')
而後分割cookie爲數組 (cookie之間用的是分號分隔)code
var cookies = document.cookie.split(';');
而後遍歷數組,獲取當前cookie名字,判斷是否等於你要的cookie值的名字, (cookie存儲爲cookieName=cookieValue這種形式)string
var cookie = cookies[i];//獲得當前cookie名字 if (cookie.substring(0, cookieName.length + 2).trim() == cookieName.trim() + "=")
相等的話,則獲取cookie值,接着跳出循環it
cookieValue = cookie.substring(cookieName.length + 2, cookie.length); break;