

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to read a cookie using JavaScript?
Following is the code to read a cookie using JavaScript −
Note − A local server would be needed to run this example.
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: blueviolet } </style> </head> <body> <h1>Read a cookie using JavaScript</h1> <div class="result"></div> <button class="Btn">Click here</button> <h3>Click on the above button to read the cookie</h3> <script> let BtnEle = document.querySelector(".Btn"); let resEle = document.querySelector(".result"); document.cookie = "Name=Rohan Sharma"; BtnEle.addEventListener("click", () => { resEle.innerHTML = document.cookie; }); </script> </body> </html>
Output
On clicking the ‘CLICK HERE’ button −
- Related Questions & Answers
- How to set a cookie and get a cookie with JavaScript?
- How do I create and read a value from the cookie in JavaScript?
- Use IFrame and read cookie in ABAP
- How to test if a JavaScript cookie has expired?
- How to read and write a file using Javascript?
- How to set JavaScript Cookie with no expiration date?
- How to read data from *.CSV file using JavaScript?
- How to read data from JSON array using JavaScript?
- How to set cookie value with AJAX request in JavaScript?
- How to read a HTTP header using JSP?
- How to share Session ID Cookie with another request using Postman?
- How to read data from a file using FileInputStream?
- How to read form data using JSP?
- How to read a file from command line using Python?
- How to read contents of a file using Scanner class?
Advertisements