Reputation: 29
I was writing this code that is based on a tutorial and on the tutorial the code works, but when I try to do it by myself it doesn't. Can you help me ?
The code : (Tutorial Link).
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var user_name;
var expires = new Date();
if ((document.cookie == "") == false)
{
var length = document.cookie.length - 1;
var message = document.cookie.substr(5, length);
document.write("<h2><center>Welcome back, " + message + "</center></h2>");
}
function check()
{
user_name = document.getElementById("name").value;
expires.setFullYear(expires.getFullYear() + 1);
document.cookie = escape("name") + escape(user_name) + "; expires = " + expires.toGMTString();
alert(document.cookie);
}
</script>
<meta charset="utf-8">
<title></title>
</head>
<body>
Name: <input type="text" id="name" />
<input type="button" value="Enter" onClick="check()" />
</body>
</html>
Upvotes: 1
Views: 146
Reputation: 146249
Because you've mentioned I was creating this code that is tutorial based and on the tutorial
, so if it's just for a test myself
thing then you may consider to use this one (Demo Here), just enter a name and value for the cookie in the cookie name and cookie value fields respectively, when you want to Add
a new cookie and to Delete
a cookie just enter the name of the cookie in the cookie name field and same for View
, you can add a Delete All
buttons only by splitting cookies using &
and making a loop and calling the eraseCookie()
function, try it yourself.
Also there are already better ones are available on the web to use in the real case, but this one is not bad at all.
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
return true;
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
if(!name) {
alert('\nPlease enter the name of cookie in cookie the name field.');
return false;
}
if(readCookie(name)){
if(createCookie(name,"",-1)) {
alert('Cookie "' + name + '" has been deleted!');
}
}
else alert('Cookie "' + name + '" doesn\'t exist!');
}
function addCookie()
{
cookie_name = document.getElementById("cName").value.replace(/^\s+|\s+$/g,'');
cookie_value = document.getElementById("cValue").value.replace(/^\s+|\s+$/g,'');
if(cookie_name.length && cookie_value.length){
createCookie(cookie_name, cookie_value, 7);
alert("New cookie has been added, \ncookie name : " + cookie_name + "\ncookie value : " + cookie_value);
}
else{
alert("Please enter a name and value for the cookie.");
}
}
function showCookie(name)
{
if(!name) {
alert('\nPlease enter the name of cookie in the cookie name field.');
return false;
}
var val = readCookie(name);
if(val){
alert(readCookie(name));
}
else alert('Cookie "' + name + '" doesn\'t exist!');
}
// Onload to check and greet
if(name = readCookie('name')) alert('Welcome back, ' + name);
Upvotes: 1