Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Created July 5, 2023 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cferdinandi/be30070de9f805427c533b68ef4ab190 to your computer and use it in GitHub Desktop.
Save cferdinandi/be30070de9f805427c533b68ef4ab190 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>getCookie()</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script>
/**
* Get the value of a cookie
* Source: https://gist.github.com/wpsmith/6cf23551dd140fb72ae7
* @param {String} name The name of the cookie
* @return {String} The cookie value
*/
function getCookie (name) {
let value = '; ' + document.cookie;
let parts = value.split(`; ${name}=`);
if (parts.length == 2) return parts.pop().split(';').shift();
}
// Set a cookie named sandwich, with a value of turkey
// Cookie expires on December 31, 2024 at 11:59 and 59 seconds PM
document.cookie = 'sandwich=turkey;';
// returns "turkey"
let sandwich = getCookie('sandwich');
console.log(sandwich);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment