February 2018
Intermediate to advanced
298 pages
8h 22m
English
JavaScript doesn't provide any convenient way out of the box to actually get a cookie value. All we have is just a bunch of random cookies together in a string, which is accessible by document.cookie. We will need to do some work, as shown in the following snippet:
document.cookie = "awesomecookie=yes;";document.cookie = "ilovecookies=sure;";document.cookie = "great=yes";function getCookie(name) { const cookies = document.cookie.split(';'); for(let i=0;i < cookies.length;i++) { if(cookies[i].trim().indexOf(name) === 0) { return cookies[i].split('=')[1]; } } return null;}console.log(getCookie("ilovecookies"));console.log(getCookie("doesnotexist"));
The output will be, therefore, as follows:
surenull
As you can see in ...
Read now
Unlock full access