JavaScript Code


How to get all cookies for the web page?


Tutorials using JS

Get all cookies

In this tutorial, you shall learn how to get all cookies for the document/web-page.

Solution

To get all the cookies currently present for the document, use document.cookie. cookie property returns a semicolon separated list of name-value pairs as shown in the following.

key1=value1; key2=value2; key3=value3

Program

In the following program, we get the cookies string using JavaScript, and then display this value in div#output element.

<!DOCTYPE html>
<html>
<body>
<h1>Cookies</h1>
<div id="output"></div>

<script>
var cookies = document.cookie;
document.getElementById('output').innerHTML = cookies;
</script>

</body>
</html>

Copyright @2022