Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
159
rated 0 times [  160] [ 1]  / answers: 1 / hits: 21340  / 12 Years ago, wed, august 8, 2012, 12:00:00

I've created a Django website, and need a cookie to be stored and readable from any part of the site. The javascript for it is in every part I need it in, but for some reason the cookie itself is stored seperately for each page. E.g. if the cookie is equal to set on one page, it can be undefined on another. Here's the code I'm using to create, get, and read the cookie (the createBannerCookie() method is called when a specific button, found on every page, is pressed)-



<script type=text/javascript>
$(document).ready(function() {
$('#banner').hide();
checkBannerCookie();
});

function createBannerCookie()
{
$('#banner').hide();
var exdate=new Date();
exdate.setDate(exdate.getDate() + 3);
var c_value=escape(set) + ((exdate==null) ? : ; expires=+exdate.toUTCString());
document.cookie='banner=' + 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 checkBannerCookie()
{
var banner=getCookie(banner);
if (banner!=null && banner!=)
{
$('#banner').hide();
}
else
{
$('#banner').show();
}
}
</script>


Any suggestions?


More From » cookies

 Answers
35

By default, cookies are accessible only to web pages in the same directory as the web page which originally created the cookie. Please try to add path=/ option. e.g.



document.cookie =
'propertyName=test; path=/'

[#83757] Tuesday, August 7, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazmyne

Total Points: 503
Total Questions: 102
Total Answers: 99

Location: Svalbard and Jan Mayen
Member since Sun, Sep 25, 2022
2 Years ago
;