Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #39809
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Create cookie from ID of <a> tag |
| Date | 2021-03-31 00:58 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <11728310.O9o76ZdvQC@PointedEars.de> (permalink) |
| References | (7 earlier) <2578953.mvXUDI8C0e@PointedEars.de> <slrns5s5ue.49m.jon+usenet@raven.unequivocal.eu> <4641170.31r3eYUQgx@PointedEars.de> <2196370.ElGaqSPkdT@PointedEars.de> <fce95a29-cdfe-4703-922c-0d296b137310n@googlegroups.com> |
Michael Haufe (TNO) wrote:
> On Friday, March 26, 2021 at 5:22:39 PM UTC-5, Thomas 'PointedEars' Lahn
> wrote:
>> As I keep thinking about this, one would hope that after all this time
>> that cookies have been around, and given my impression that cookies are
>> not going away anytime soon, a standard DOM attribute and built-in
>> property will finally be introduced to make dealing with cookies easier
>> and less error- prone.
>
> I've use the following for quite some time. I'd write it a bit differently
> today but it's been a good enough solution thus far for me:
>
> <https://gist.github.com/mlhaufe/01cc0d183774b3444f89447004fd9607>
Thank you. My retrieval/storage code (specifically written backwards
compatible) looks similar, but I found it a good idea to include the
possibility of the cookie data not being live so as to make access to
it more efficient.
If you have suggestions for improvement, please let me know:
-- cookie.js ------------------------------------------------------------
/**
* This library is intended to provide easy access to cookies.
*
* Read cookie by name:
* cookies['$NAME'] === value
* Save cookie/update value/properties:
* cookies['name'] = value
* cookies['name'] = {value: value, properties}
* cookies['name'].property = propertyValue [not yet implemented]
* Delete cookie (set expires to 0):
* delete cookies['name'] (for the current domain)
* cookies.removeItem('name', {domain: '$DOMAIN', properties})
* (for allowed domains)
* @type {Object}
*
* // begin WebIDL
*
* [Exposed=jsx.dom]
* interface mixin DocumentStorage {
* readonly attribute Cookies cookies;
* }
*
* [Exposed=DocumentStorage]
* interface Cookies {
* readonly attribute unsigned long length;
* DOMString? name(unsigned long index);
* sequence <DOMString> names();
* sequence <DOMString> values();
* sequence <record <DOMString, CookieAttributes>> items();
* getter DOMString? getItem(DOMString name);
* boolean hasItem(DOMString name);
* setter boolean setItem(DOMString name, DOMString value);
* setter boolean setItem(DOMString name, CookieAttributes attributes);
* deleter boolean removeItem(DOMString name);
* boolean clear();
* }
*
* dictionary CookieAttributes {
* required DOMString value;
* DOMString path = Location::pathname;
* unsigned long? maxAge = null; // default: end of session
*
* // or unsigned long, but Date would be more convenient
* Date? expires = null; // default: at the end of the session
*
* DOMString domain = Location::hostname;
* boolean secure = false;
* boolean sameSite = false;
* }
*
* // end WebIDL
*/
/*eslint-env es6 */
jsx.dom.cookies = {
/** Internal dictionary of cookies */
_items: Object.create(null),
/**
* Determines whether this object uses live data.
*
* If <code>true</code>, methods of this object update the internal
* dictionary from <code>document.cookie</code>; otherwise they only
* read from the internal dictionary which can be updated manually
* by calling {@link #update()}.
*
* Set this property to <code>false</code> to make repeated access
* more efficient.
*
* @type {Boolean}
*/
live: true,
Cookie: function (properties) {
var proto = Object.getPrototypeOf(this);
Object.keys(properties).forEach(function (key) {
if (key in proto && (typeof proto[key] != 'function')) {
var value = properties[key];
var setterName = 'set' + key.charAt(0).toUpperCase()
+ key.substring(1);
if (typeof this[setterName] == 'function') {
this[setterName](value);
}
else {
this[key] = value;
}
}
}.bind(this));
}.extend(null, {
name: '',
value: null,
path: '',
maxAge: null,
expires: null,
domain: '',
secure: false,
sameSite: '',
setMaxAge: function (value) {
this.maxAge = parseInt(value, 10);
},
setExpires: function (value) {
this.expires = (value instanceof Date)
? value
: new Date(value);
},
setSecure: function (value) {
this.secure = !!value;
},
setSameSite: function (value) {
if ([void 0, 'lax', 'strict', 'none'].indexof(value) < 0) {
throw new jsx.InvalidArgumentError(
'Expected: samesite=(lax | strict | none)');
}
this.sameSite = value;
},
toString: function () {
return [this.name, this.value].map(encodeURIComponent).join('=')
+ (this.path ? ';path=' + this.path : '')
+ (this.maxAge ? ';max-age=' + this.maxAge : '')
+ (this.expires ? ';expires=' + this.expires.toUTCString() : '')
+ (this.domain ? ';domain=' + this.domain : '')
+ (this.secure ? ';secure' : '')
+ (this.sameSite ? ';samesite=' + this.sameSite : '');
}
}),
/**
* Update the internal dictionary from <code>document.cookie</code>.
*
* @return {Object} The updated dictionary
*/
update: function () {
this._items = Object.create(null);
var cookies = document.cookie;
if (cookies.indexOf('=') > 0) {
cookies.split(/\s*;\s*/)
.forEach(function (cookie) {
var parts = cookie.split('=').map(decodeURIComponent);
var name = parts[0];
var value = parts.slice(1).join('=');
this._items[name] = new this.Cookie({name: name, value: value});
}.bind(this));
}
return this._items;
},
items: function () {
if (this.live) this.update();
return this._items;
},
/** @return int */
length: function () {
return this.names().length;
},
/** @return String */
name: function (/** int */ index) {
return this.names()[index];
},
/** @return Array[String] */
names: function () {
return Object.keys(this.items());
},
/** @return sequence <DOMString> */
values: function () {
return this.names().map(function (name) {
return this.getItem(name).value;
}.bind(this));
},
/** @return getter DOMString? */
getItem: function (/** DOMString */ name) {
return this.items()[name];
},
/** @return boolean */
hasItem: function (/** DOMString */ name) {
return (name in this.items());
},
/** @type setter boolean */
setItem: function (/** DOMString */ name, /** DOMString */ value) {
var cookie;
if (jsx.object.isObject(value)) {
cookie = new this.Cookie(value);
}
else {
cookie = new this.Cookie({name: name, value: value});
}
this._items[name] = cookie;
document.cookie = cookie.toString();
},
/**
* Delete a cookie
*
* @deleter
* @param {DOMString} name
* @return {[type]}
* <code>true</code> if successful, <code>false</code> otherwise.
*/
removeItem: function (name) {
document.cookie = (new this.Cookie({
name: name,
expires: 0
})).toString();
this.update();
return !this.hasItem(name);
},
/** @return boolean */
clear: function () {
this.names().forEach(function (name) {
this.removeItem(name);
}.bind(this));
}
};
if (typeof Proxy == 'function')
{
jsx.dom.cookies = new Proxy(jsx.dom.cookies, {
get: function (target, propertyName) {
if (propertyName in target)
{
return target[propertyName];
}
return target.getItem(propertyName);
},
set: function (target, propertyName, value) {
if (propertyName in target)
{
target[propertyName] = value;
}
else {
target.setItem(propertyName, value);
}
},
deleteProperty: function (target, propertyName) {
if (!(propertyName in target))
{
return target.removeItem(propertyName);
}
}
});
}
-------------------------------------------------------------------------
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Create cookie from ID of <a> tag Daniele Ferrarese <daniele.seversrl@gmail.com> - 2021-03-24 09:36 -0700
Re: Create cookie from ID of <a> tag The Natural Philosopher <tnp@invalid.invalid> - 2021-03-24 17:54 +0000
Re: Create cookie from ID of <a> tag Daniele Ferrarese <daniele.seversrl@gmail.com> - 2021-03-25 01:05 -0700
Re: Create cookie from ID of <a> tag The Natural Philosopher <tnp@invalid.invalid> - 2021-03-25 09:13 +0000
Re: Create cookie from ID of <a> tag Daniele Ferrarese <daniele.seversrl@gmail.com> - 2021-03-25 02:21 -0700
Re: Create cookie from ID of <a> tag Daniele Ferrarese <daniele.seversrl@gmail.com> - 2021-03-25 03:23 -0700
Re: Create cookie from ID of <a> tag Daniele <webmaster@appliserver.com> - 2021-03-26 09:19 +0100
Re: Create cookie from ID of <a> tag Daniele Ferrarese <daniele.seversrl@gmail.com> - 2021-03-26 01:57 -0700
Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-03-26 17:33 +0100
Re: Create cookie from ID of <a> tag Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-26 17:20 +0000
Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-03-26 19:35 +0100
Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-03-26 23:22 +0100
Re: Create cookie from ID of <a> tag "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-26 15:39 -0700
Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-03-31 00:58 +0200
Re: Create cookie from ID of <a> tag "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-30 19:58 -0700
Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-01 10:09 +0200
Re: Create cookie from ID of <a> tag "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-04-01 06:11 -0700
Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-01 19:34 +0200
Re: Create cookie from ID of <a> tag The Natural Philosopher <tnp@invalid.invalid> - 2021-03-26 17:10 +0000
Re: Create cookie from ID of <a> tag Daniele <webmaster@appliserver.com> - 2021-03-27 12:16 +0100
Re: Create cookie from ID of <a> tag The Natural Philosopher <tnp@invalid.invalid> - 2021-03-27 13:25 +0000
Re: Create cookie from ID of <a> tag "Mark-" <nospam@mapson.xyz> - 2021-03-31 13:26 +0000
Re: Create cookie from ID of <a> tag Daniele <webmaster@appliserver.com> - 2021-03-27 12:08 +0100
Re: Create cookie from ID of <a> tag Man From a city with name Viljandi <ya12983@mail.com> - 2021-03-28 06:26 -0700
csiph-web