Welcome to WebmasterWorld Guest from 54.196.17.193

Forum Moderators: open

Message Too Old, No Replies

Set Cookie With Javascript

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

Junior Member

5+ Year Member

joined:Aug 7, 2007
posts:45
votes: 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.

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

Junior Member

10+ Year Member

joined:Dec 7, 2004
posts:130
votes: 0


Hi Dragosh,

Just remove the quotes around the second argument.

HTH, AA

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

Junior Member

5+ Year Member

joined:Aug 7, 2007
posts:45
votes: 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 ;(
7:48 am on Aug 20, 2007 (gmt 0)

Senior Member from US 

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

joined:Aug 26, 2004
posts:3168
votes: 22


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)

Senior Member from US 

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

joined:Oct 17, 2005
posts:4996
votes: 14



<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() {
SetCookie('category',document.getElementById('selection').value,'10');
};
}
</script>
<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)

Junior Member

5+ Year Member

joined:Aug 7, 2007
posts:45
votes: 0


THanks Fotiman!
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members