Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Set Cookie With Javascript



2:34 pm on Aug 19, 2007 (gmt 0)

5+ Year Member

this is the fuction i use to set a cookie:

function SetCookie(cookieName,cookieValue,nDays) {
var today = new Date();
var expire = new Date();
if (nDays==null ¦¦ nDays==0) nDays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString();

this is the button which sets the cookie

<input value="Show" type="button" onClick="SetCookie('category','document.getElementById('selection').value','10'" />

the value 'selection' is taken from a select box with id selection.
Howether this script doesnt work. Please tell me where i made a mistake.


6:37 am on Aug 20, 2007 (gmt 0)

10+ Year Member

Hi Dragosh,

Just remove the quotes around the second argument.



7:34 am on Aug 20, 2007 (gmt 0)

5+ Year Member

It doesnt work. Javascript doesnt set cookie.
I dont know if it could be the problem but before i click the button to set the cookie with javascript a php code already set the cookie to default value all. What i want is to set the cookie value to another value with javascript but it seems php and javascript cookies dont like each other ;(

JAB Creations

7:48 am on Aug 20, 2007 (gmt 0)

WebmasterWorld Senior Member jab_creations is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Dragosh, Webkit has never in all my days "allowed" my JavaScript to create cookies. I'd recommend setting your cookies via a serverside language if possible.

- John


3:22 pm on Aug 20, 2007 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

<input value="Show" type="button" onClick="SetCookie('category','document.getElementById('selection').value','10'" />

The problem is your use of quotes (specifically, you have single quotes nested within single quotes), and you forgot the closing parenthesis on the call to SetCookie. You don't need to quote the document.getElementById portion. Try this:

<input value="Show" type="button" onClick="SetCookie('category', document.getElementById('selection').value,'10')" />

A better fix (and it's also good practice) is to use unobtrusive JavaScript. Instead of defining your onclick event handler inline in your HTML, keep your markup clean and instead define the handler in a script.

<script type="text/javascript">
// Perform this when the window loads
// Note, for this example I'm using window.onload which
// may overwrite any other onload event handlers that
// you have, so combine them all into one or (better yet)
// use something like the Yahoo UI Library's Event Utility
// to attach the event handler
window.onload = function() {
var showButton = document.getElementById('showButton');
showButton.onclick = function() {
<input value="Show" type="button" id="showButton" />

[edited by: Fotiman at 3:23 pm (utc) on Aug. 20, 2007]


6:44 am on Aug 21, 2007 (gmt 0)

5+ Year Member

THanks Fotiman!

Featured Threads

Hot Threads This Week

Hot Threads This Month