Forum Moderators: open
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.
<input value="Show" type="button" onClick="SetCookie('category','document.getElementById('selection').value','10'" />
<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]