CSS Practical No 10
CSS Practical No 10
<div id="cookieOutput"></div>
<script>
function setCookie() {
const name = document.getElementById('cookieName').value;
const value = document.getElementById('cookieValue').value;
const days = parseInt(document.getElementById('cookieDays').value);
const expires = new Date();
expires.setTime(expires.getTime() + (days * 24 * 60 * 60 * 1000)); // Set expiry time
const expiresStr = `expires=${expires.toUTCString()}`;
function getCookie() {
const name = document.getElementById('cookieName').value;
const cookieName = `${name}=`;
const decodedCookie = decodeURIComponent(document.cookie);
const cookiesArray = decodedCookie.split(';');
function deleteCookie() {
const name = document.getElementById('cookieName').value;
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
document.getElementById('cookieOutput').innerHTML = `Cookie '${name}' deleted
successfully.`;
}
</script>
</body>
</html>
Output:
Q.2) Program to create session cookies.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Enhanced Session Cookies</title>
<script>
// Function to create a session cookie
function createCookie(name, value) {
if (name && value) {
document.cookie = `${name}=${value}; path=/;`;
alert(`Cookie "${name}" created!`);
displayCookies();
} else {
alert("Please enter both cookie name and value.");
}
}
<h2>Current Cookies:</h2>
<ul id="cookieList">
<!-- Cookies will be dynamically listed here -->
</ul>
</body>
</html>
Output: