/    Sign up×
Community /Pin to ProfileBookmark

setCookie is not defined??!

Hello ive been getting an error creating a simple cookie and i have no idea why… heres my sample code.

<script type=”text/javascript”>

function saveIt()
{
var x = prompt(“Enter value”);

if (!x)
alert(‘Please fill in a value in the input box.’);
else
{
alert(‘entered’);
setCookie(‘abcdefo’,x);
alert(‘Cookie created’);
}
}
</script>

and saveIt() is called via…

<a href=”javascript:saveIt()”>Create cookie 1</a>

i realise this is terrible code practicing as im not even checking if the cookie already exists but in this case i know it doesnt therefore i dont understand the error. perhaps my syntax is messed somewhere and i just cant see it. also, when i enter a value it does get to the alert(‘entered’); so it crashes on the setCookie.. i have no idea whats wrong

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Calvert_TripesiFeb 27.2011 — There's no need to worry about checking that a cookie exists before overwriting it.

Where is your [I]setCookie[/I] function defined?
Copy linkTweet thisAlerts:
@windmillauthorFeb 28.2011 — I dont, it was just a simple test program. Is there no default syntax in javascript which i can just use? cause i know name and value are necessary but the rest is optional is it not?
Copy linkTweet thisAlerts:
@Calvert_TripesiFeb 28.2011 — I dont, it was just a simple test program. Is there no default syntax in javascript which i can just use? cause i know name and value are necessary but the rest is optional is it not?[/QUOTE]There is no built-in function [FONT="Courier New"]setCookie[/FONT]. You could code:[CODE]document.cookie = 'abcdefo=' + x;[/CODE], which would create a session cookie, but to set expiry time and possibly other parameters, a function makes things easier. There are many examples of varying quality to be found, but you may want to try writing your own
Copy linkTweet thisAlerts:
@windmillauthorFeb 28.2011 — so i would code that instead of setCookie()? sorry im kinda really new with cookies
×

Success!

Help @windmill 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.16,
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,
)...