forked from davidflanagan/jstdg7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetCookies.js
16 lines (16 loc) · 856 Bytes
/
getCookies.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Return the document's cookies as a Map object.
// Assume that cookie values are encoded with encodeURIComponent().
function getCookies() {
let cookies = new Map(); // The object we will return
let all = document.cookie; // Get all cookies in one big string
let list = all.split("; "); // Split into individual name/value pairs
for(let cookie of list) { // For each cookie in that list
if (!cookie.includes("=")) continue; // Skip if there is no = sign
let p = cookie.indexOf("="); // Find the first = sign
let name = cookie.substring(0, p); // Get cookie name
let value = cookie.substring(p+1); // Get cookie value
value = decodeURIComponent(value); // Decode the value
cookies.set(name, value); // Remember cookie name and value
}
return cookies;
}