/    Sign up×
Community /Pin to ProfileBookmark

Really Anoying Cookie Problem

Hi i have a cookie script on my homepage as follows

<script type=”text/javascript”><!–
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (“;”, offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
var arg = name + “=”;
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(” “, i) + 1;
if (i == 0) break;
}
return null;
}

function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + “=” + escape (value) +
((expires == null) ? “” : (“; expires=” + expires.toGMTString())) +
((path == null) ? “” : (“; path=” + path)) +
((domain == null) ? “” : (“; domain=” + domain)) +
((secure == true) ? “; secure” : “”);
}

// –>
</script>

The cookie remembers a colour selected on that page and tracks it through various pages in that domain however when i go to a subdomain it seems to forget, any ideas how i can solve this??

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@bathurst_guySep 06.2005 — it doesnt forget, you have set it to only activate the cookie on that path.
Copy linkTweet thisAlerts:
@educationauthorSep 06.2005 — How do i go about doing that cause i downloaded the script and have no idea how to code it??
Copy linkTweet thisAlerts:
@bathurst_guySep 06.2005 — well what if you just dont set the path, it might work for the whole domain...?
[CODE]
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
[/CODE]
Copy linkTweet thisAlerts:
@educationauthorSep 06.2005 — right do i delete any lines in my scrit or just the lines you printed, if so where??
Copy linkTweet thisAlerts:
@bathurst_guySep 06.2005 — replace the whole lot with this. I'm not too sure it will work though, but its all about trial and error...
[CODE]
<script type="text/javascript"><!--
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
// -->
</script>
[/CODE]
Copy linkTweet thisAlerts:
@educationauthorSep 06.2005 — right ive gotta go out, shoudl be back at 18:00 GMT so anybody that has any ideas please post replys and ill read them when i get in thanks everyone.
Copy linkTweet thisAlerts:
@educationauthorSep 06.2005 — right ive tried the new code and it doesnt work, does anyone have any ideas, this is my last hurdle i beleive and if you can help me i would be very greatful
×

Success!

Help @education spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...