/    Sign up×
Community /Pin to ProfileBookmark

Create Cookie with SubKeys

Does anyone have some sample code that creates a cookie with subkeys?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@dch3authorJul 02.2009 — After pulling out what's left of my hair and just dumb luck...

function setCookie(expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
cookieValue = "DefaultDirectoryType=" + document.getElementById("DirectoryTypeInput").value + "&DefaultDirectoryName=" + document.getElementById("DirectoryNameInput").value + "&DefaultSortOrder=" + document.getElementById("SortOrderInput").value;
window.alert(cookieValue);
document.cookie = "ContactDirectoryDefaults" + "=" + cookieValue + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}


The key line being...

document.cookie = "ContactDirectoryDefaults" + "=" + cookieValue + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());

Where 'ContactDirectoryDefaults' is used as the 'name' and the string of subkeys is used as the corresponding value as in...

document.cookie ="MyCookie=Car1=Honda&Car2=Ford&Car3=GMC"

Each subkey is delimited with a '&'.

No idea if this works for other browsers, but its working for my environment IE6 & ASP.NET.

Keywords/Phrases: javascript, asp.net, cookie, cookies, multiple values, subkeys, javascript equivalent, store multiple values in one cookie, Javascript Write Multiple values in a Cookie
×

Success!

Help @dch3 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...