Created
September 17, 2017 19:35
-
-
Save Arieg419/4f7fe0fb96ac16c109ec8e3e6c7dbb05 to your computer and use it in GitHub Desktop.
Web Cookies for Everyone example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script> | |
const setCookie = (key,val,days_till_expiration) => { | |
let d = new Date(); | |
d.setTime(d.getTime() + (days_till_expiration*24*60*60*1000)); | |
const expires = "expires=" + d.toGMTString(); | |
document.cookie = key + "=" + val + ";" + expires + ";"; | |
} | |
const fetchCookie = () => { | |
const name = "username" + "="; | |
const cookie_array = document.cookie.split(';'); | |
for(let i = 0; i < cookie_array.length; i++) { | |
let c = cookie_array[i]; | |
if (c.indexOf(name) == 0) { | |
return c.substring(name.length, c.length); | |
} | |
} | |
return ""; | |
} | |
const getUserData = () => { | |
let username = fetchCookie(); | |
if (username != "") { | |
alert("Welcome again " + username); | |
} else { | |
username = prompt("Please enter your name:",""); | |
setCookie("username", username, 30); | |
} | |
} | |
const alertCookie = () => { | |
alert(document.cookie); | |
} | |
</script> | |
</head> | |
<body> | |
<h1>Cookies for everyone!</h1> | |
<button onclick="getUserData()">Check for Cookies</button> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment