Get list of all Browser cookies using JavaScript
Cookies are mainly data’s that are stored in the computer for browser accessing. For making the work easy you can make all these cookies in a single page using JavaScript. We are mainly using these Http cookies to store data on our visitor’s browser. Directory path or page path have important role in setting these cookies, try to use the “path” parameter for setting cookies. Please have a look at the code below, this code will help you to set all cookies to a single page.
The below code explains about adding the cookie name as the index & for the cookie value as the value itself:
function get_cookies_array() {
var cookies = { };
if (document.cookie && document.cookie != '') {
var split = document.cookie.split(';');
for (var i = 0; i < split.length; i++) {
var name_value = split[i].split("=");
name_value[0] = name_value[0].replace(/^ /, '');
cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
}
}
return cookies;
}
var cookies = get_cookies_array();
for(var name in cookies) {
console.log( name + " : " + cookies[name] + "
" );
}
It is over, what you have do now is simply copy & past this line of codes in console & run
Some of the major things that we are used in JavaScript with cookies are listed below.
adminadminalthaf
How to set Cookie in JavaScript
Create JavaScript cookies with the help of document is listed below
document.cookie = “cookiename=cookievalue”
For some cookies we will use expiry dates, if you want to add expiry date for any particular cookies please have a look at the below example. There is no need to worry cookies will be automatically remove from the browser when the browser is closed. Expiry date can be added as below.
document.cookie = “cookiename=cookievalue; expires= Thu, 23 June 2019 23:15:00 UTC”
How to get Cookie in JavaScript
Domain names are saved by the cookies that we set & we can get its access by setting the cookie code.
var x = document.cookie
How to Delete Cookie in JavaScript
For deleting a cookie in java script, set the cookie value to empty and set the value of expires to a passed date.
document.cookie = “cookiename= ; expires = wed, 06 Mar 1980 00:00:00 GMT”