+1 vote
178 views
by (98.9k points)
B Write a JavaScript code to set a cookie on the user's computer. 05

1 Answer

0 votes
by (98.9k points)
 
Best answer
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Set Cookie Example</title>
</head>
<body>

<script>
  function setCookie(name, value, daysToExpire) {
    const date = new Date();
    date.setTime(date.getTime() + (daysToExpire * 24 * 60 * 60 * 1000));
    const expires = `expires=${date.toUTCString()}`;
    document.cookie = `${name}=${value}; ${expires}; path=/`;
  }

  // Example usage:
  setCookie('user', 'John Doe', 7);
</script>

</body>
</html>

Related questions

+1 vote
1 answer 49 views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

504 users

...