जावास्क्रिप्ट का उपयोग करके कुकी को पढ़ने के लिए कोड निम्नलिखित है -
नोट - इस उदाहरण को चलाने के लिए एक स्थानीय सर्वर की आवश्यकता होगी।
उदाहरण
<!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> आउटपुट

'यहां क्लिक करें' बटन पर क्लिक करने पर -
