/    Sign up×
Community /Pin to ProfileBookmark

What does this do? document.cookie.substring(,)

Hello what does this command do? Googled many pages of it, doesn’t say what it does. document.cookie.substring(,)

Thank you

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoSep 16.2008 — Sorry, read your post wrong (split instead of substring). most likely it was meant ot be split(','). substring(,) should do nothing, or in some browsers throw and error, because both arguments will be null.
Copy linkTweet thisAlerts:
@rnd_meSep 16.2008 — it returns the whole cookie as a string.
Copy linkTweet thisAlerts:
@vortexerauthorSep 16.2008 — Thank you, is there an easy way to get it to show the first value of the cookie only, the cookie name? Or is there another easy way to do that.

Thanks you very much for your help.
Copy linkTweet thisAlerts:
@rnd_meSep 17.2008 — Thank you, is there an easy way to get it to show the first value of the cookie only, the cookie name? Or is there another easy way to do that.

Thanks you very much for your help.[/QUOTE]

[CODE]
var firstCookieName = document.cookie.toString().split(/;/g)[0].split("=")[0];
alert(firstCookieName);
[/CODE]
Copy linkTweet thisAlerts:
@A1ien51Sep 17.2008 — If you search for [I]getCookie JavaScript[/I], you should get plenty of examples.

Eric
Copy linkTweet thisAlerts:
@vortexerauthorSep 17.2008 — Thanks I have done lots of googles for it already, only shows long functions. Came here to try to find something shorter.

var firstCookieName = document.cookie.toString().split(/;/g)[0].split("=")[0];

alert(firstCookieName);[/QUOTE]


This is working well but its only returning one cookie name. Would if a page has a few cookie names already on it. How can I get it to show any cookie names for a page. The first cookie that is in the cookie variable of

setCookie(allofthesecookienames, don'tneedcookievalue,don'tneedexpiration)

Please let me know thank you very much for your help.
Copy linkTweet thisAlerts:
@rnd_meSep 17.2008 — [CODE]
//compatibility code:
if (!Array.prototype.map) {// from http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:map
Array.prototype.map = function (fun) {var len = this.length;if (typeof fun != "function") {throw new TypeError;}var res = new Array(len);var thisp = arguments[1];for (var i = 0; i < len; i++) {if (i in this) {res[i] = fun.call(thisp, this[i], i, this);}}return res;};}

//returns array of all cookie names:
document.cookie.toString().split(/;/g).map(function(a){return a.split("=")[0];})

[/CODE]
Copy linkTweet thisAlerts:
@vortexerauthorSep 17.2008 — Thanks tried it like this

<script type="text/javascript">

if (!Array.prototype.map) {

Array.prototype.map = function (fun) {var len = this.length;if (typeof fun != "function") {throw new TypeError;}var res = new Array(len);var thisp = arguments[1];for (var i = 0; i < len; i++) {if (i in this) {res[i] = fun.call(thisp, this[i], i, this);}}return res;};}

document.cookie.toString().split(/;/g).map(function(a){return a.split("=")[0];})

</script>
[/QUOTE]


Not working, anyone know of a real easy way to read cookie names without a real long function? Please let me know thank you very much for your help.
Copy linkTweet thisAlerts:
@rnd_meSep 17.2008 — Thanks tried it like this


Not working, anyone know of a real easy way to read cookie names without a real long function? Please let me know thank you very much for your help.[/QUOTE]


i just re-tested in FF3, IE7. might want to take another look.
Copy linkTweet thisAlerts:
@vortexerauthorSep 18.2008 — Thanks some reason not working for me in any browser but no error messages either. Have the script above the last </head> tag in a regular .html page. Even set the cookie name vv first. Still nothing.
<script type="text/javascript">

function setCookie(name, value, days)

{var expires = "", date = new Date(); if (days)

{expires = ";expires=" + new Date(date.setDate(date.getDate() + days)).toGMTString();}


document.cookie = name + "=" + value + expires + "; path=/"; } setCookie('vv', 'vvv', 365);

if (!Array.prototype.map) {

Array.prototype.map = function (fun) {var len = this.length;if (typeof fun != "function") {throw new TypeError;}var res = new Array(len);var thisp = arguments[1];for (var i = 0; i < len; i++) {if (i in this) {res[i] = fun.call(thisp, this[i], i, this);}}return res;};}

document.cookie.toString().split(/;/g).map(function(a){return a.split("=")[0];})

</script>
[/QUOTE]
Copy linkTweet thisAlerts:
@rnd_meSep 18.2008 — Thanks some reason not working for me in any browser but no error messages either. Have the script above the last </head> tag in a regular .html page. Even set the cookie name vv first. Still nothing.[/QUOTE]

your date is being set to either the current time or an invalid date, and thus; expires immediately.

months only go up to 31 days.

the way your script is coded, you try to set the day of month to 383 (for sep 18th).

that wont work...
Copy linkTweet thisAlerts:
@vortexerauthorSep 19.2008 — Thanks, setCookie function sets expire for days. 365 in the last field means won't expire until a year from now so should work.
Copy linkTweet thisAlerts:
@rnd_meSep 19.2008 — Thanks, setCookie function sets expire for days. 365 in the last field means won't expire until a year from now so should work.[/QUOTE]

yes, it [U]should [/U]set the expiry a year from now, it [U]doesn[/U]'t.

from your setCookie:
[CODE]expires = ";expires=" + new Date(date.[B][U][COLOR="Red"]setDate[/COLOR][/U][/B](date.getDate() + [B][U][COLOR="Red"]days[/COLOR][/U][/B])).toGMTString()[/CODE]


check out the docs on setDate for a second opinion.
×

Success!

Help @vortexer 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.28,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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