/    Sign up×
Community /Pin to ProfileBookmark

set and delete cookie

I am trying to set a cookie on page load (popup window), and delete it when the window is closed. Below is the code I am using, the cookie is set on page load, but I can’t get it to delete.
Please could someone look at my code and tell me where I have gone wrong.
Thanks

[code=php]
<html>
<SCRIPT>
function setCookie(isName,isValue){
document.cookie = isName+”=”+isValue;
}

function Delete(isName){
document.cookie = “regUser” + “=;expires=Thu, 01-Jan-70 00:00:01 GMT”;
}
</SCRIPT>
<body onload=”setCookie(‘regUser’,’yes’)” onbeforeunload=”Delete()”;>
popup window
</body>
</html>
[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@FangMar 24.2005 — If [i]expires[/i] has no value it will expire when the browser closes
Copy linkTweet thisAlerts:
@jennyjauthorMar 24.2005 — Thanks Fang,

setting the expiry in the past should also have the same effect.

However, that isn't the problem, I think there is something wrong with the actual code, not sure I have named everything correctly, and as I don't really know javascript, I don't know what the problem is.

I have tried not setting a value for expires, but the cookie is not deleted when the window closes.

Anyone else?

Thanks

JennyJ
Copy linkTweet thisAlerts:
@FangMar 24.2005 — This is all you need; if there's no expiry date the cookie is delete when the browser closes.
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;cookie&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
function setCookie(isName,isValue){
document.cookie = isName+"="+isValue;
}
//--&gt;
&lt;/script&gt;

&lt;/head&gt;
&lt;body onload="setCookie('regUser','yes');"&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@jennyjauthorMar 24.2005 — Just in case anyone reads this, and wants to know how I got it to work, I have posted the code below
<i>
</i>&lt;html&gt;
&lt;SCRIPT&gt;
function setCookie(){
document.cookie = 'cookie1=testcookie;'
}

function Delete(){
document.cookie = 'cookie1=testcookie; expires=Thu, 01-Jan-70 00:00:01 GMT;'
}
&lt;/SCRIPT&gt;
&lt;body onload="setCookie()" onunload="Delete()";&gt;
popup window
&lt;/body&gt;
&lt;/html&gt;



Thanks for all your help

JennyJ
Copy linkTweet thisAlerts:
@FangMar 24.2005 — if there's no expiry date the cookie is delete when the browser closes[/QUOTE]
you do not need the Delete function.
Copy linkTweet thisAlerts:
@UltimaterMar 24.2005 — Let me try explaining to him,

there are session cookies then there are normal cookies.

Normal cookies have an expiration date and will stay on the users computer for upto a year, even when the browser is closed.

Session cookies just simply delete when you close your browser.

You can make a session cookie by providing an expired date - that has passed, or entering-in a bad format. You can also do with without providing an expirition date at all.
×

Success!

Help @jennyj 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 5.8,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...