homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Set Cookie With Javascript

5+ Year Member

Msg#: 3426088 posted 2:34 pm on Aug 19, 2007 (gmt 0)

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.



5+ Year Member

Msg#: 3426088 posted 6:37 am on Aug 20, 2007 (gmt 0)

Hi Dragosh,

Just remove the quotes around the second argument.



5+ Year Member

Msg#: 3426088 posted 7:34 am on Aug 20, 2007 (gmt 0)

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

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

Msg#: 3426088 posted 7:48 am on Aug 20, 2007 (gmt 0)

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


WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member

Msg#: 3426088 posted 3:22 pm on Aug 20, 2007 (gmt 0)

<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]


5+ Year Member

Msg#: 3426088 posted 6:44 am on Aug 21, 2007 (gmt 0)

THanks Fotiman!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved