Files
lpmj6/named_examples/javascript-cookies.htm
T
RobinNixon 0335342e00 First Draft
First Draft of the Example Files
2020-12-10 13:21:38 +00:00

52 lines
1.8 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>Using JavaScript Cookies</title>
<script>
function SaveCookie(name, value, seconds, path, domain, secure)
{
var date = new Date()
date.setTime(parseInt(date.getTime() + seconds * 1000))
var expires = seconds ? '; expires=' + date.toGMTString() : ''
path = path ? '; path=' + path : ''
domain = domain ? '; domain=' + domain : ''
secure = secure ? '; secure=' + secure : ''
document.cookie = name + '=' + escape(value) + expires + path
}
function ReadCookie(name, value, seconds, path, domain, secure)
{
if (!document.cookie.length) return false
else
{
var start = document.cookie.indexOf(name + '=')
if (start == -1) return false
else
{
start += name.length + 1
var end = document.cookie.indexOf(';', start)
end = (end == -1) ? document.cookie.length : end
return unescape(document.cookie.substring(start, end))
}
}
}
function DeleteCookie(name, value, seconds, path, domain, secure)
{
SaveCookie(name, '', -60)
}
</script>
</head>
<body>
<p>The first time this page loads no cookie should have been set and the alert window should show that the cookie with the name <b>test</b> has the value <i>false</i> (meaning it is not set).</p>
<p>After you click OK a value is assigned to the cookie <b>test</b>. To see this new cookie's value click Reload.</p>
<script>
alert("The value of the cookie 'test' is: " + ReadCookie('test'))
SaveCookie('test', 'I love cookies')
</script>
</body>
</html>