Reputation: 587
Hi guys I'm trying to create cookies but with no success. Here is my site code I cant figure out why I cant get the cookie I created?
Edit site's full code added
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>yensdesign.com - How to create a stuning and smooth popup in jQuery</title>
<link rel="stylesheet" href="general.css" type="text/css" media="screen" />
<script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript"></script>
<script src="popup.js" type="text/javascript"></script>
<script type="text/javascript">
function setCookie(name,value,days)
{
var date=new Date();
var exdays = days*24*60*60*1000;
date.setDate(date.getDate() + exdays);
var c_value=escape(value) + ((days==null) ? "" : "; expires="+date.toUTCString());
document.cookie=name + "=" + c_value;
}
function getCookie(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 deleteCookie(name) {
setCookie(name,"",-1);
}
</script>
</head>
<body onload="setCookie('myCookie', 'myValue', 1);">
<center>
<a href="http://www.yensdesign.com"><img src="logo.jpg" alt="Go to yensdesign.com"/></a>
<div id="button"><input type="submit" value="Press me please!" /></div>
<div id="button"><input type="submit" value="Cookie!" onclick=getCookie('myCookie'); /></div>
</center>
<div id="popupContact"></div>
<div id="backgroundPopup"></div>
</body>
</html>
and the cookie creation that seems not working:
<body onload="setCookie('myCookie', 'myValue', 1);">
so what can be the problem here?
Upvotes: 1
Views: 315
Reputation: 587
The problem here was that cookies do not work locally with Chrome! Tried with Firefox and worked well. If you want to see it in action you have to upload your files to a server or localhost and view see how it works in Chrome.
Upvotes: 1
Reputation: 8476
try this code
function setCookie(name,value,days)
{
var date=new Date();
var exdays = days*24*60*60*1000;
date.setDate(date.getDate() + exdays);
var c_value=escape(value) + ((days==null) ? "" : "; expires="+date.toUTCString());
document.cookie=name + "=" + c_value;
}
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(';');
for (i=0; i < ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf('='));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
function deleteCookie(name) {
setCookie(name,"",-1);
}
function clickMe() {
alert(getCookie('myCookie'));
}
http://www.w3schools.com/js/js_cookies.asp
Upvotes: 0
Reputation: 31
Try this code:
<!DOCTYPE html>
<html>
<head>
<script>
function setCookie(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=/";
}
function getCookie(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 deleteCookie(name) {
setCookie(name,"",-1);
}
function clickMe() {
alert(getCookie('myCookie'));
}
</script>
</head>
<body onload="setCookie('myCookie', 'myValue', 1);">
<input type="button" onClick="clickMe()" value="test" />
</html>
Upvotes: 2