The tp.cookie object provides methods for managing browser cookies.
Methods
tp.cookie.set(name, value, options)
Sets a cookie with the specified name, value, and options.
Parameters
|
Parameter |
Type |
Required |
Description |
|---|---|---|---|
|
|
|
Yes |
The name of the cookie |
|
|
|
Yes |
The value to store |
|
|
|
No |
Configuration options (see below) |
Options Object
|
Option |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
|
URL path scope for the cookie |
|
|
|
Current domain |
Domain scope for the cookie |
|
|
|
Session |
Expiration as days (number) or Date object |
|
|
|
|
Only transmit over HTTPS |
|
|
|
|
SameSite attribute: |
|
|
|
|
Cookie priority: |
|
|
|
|
Skip URL encoding of name and value |
Example
// Basic usage
tp.cookie.set("username", "john_doe");
// With expiration (30 days)
tp.cookie.set("session_token", "abc123xyz", {
expires: 30
});
// Full options
tp.cookie.set("preferences", JSON.stringify({ theme: "dark" }), {
path: "/",
domain: ".example.com",
expires: 365,
secure: true,
samesite: "lax",
priority: "high"
});
tp.cookie.get(name)
Retrieves the value of a cookie by name.
Parameters
|
Parameter |
Type |
Required |
Description |
|---|---|---|---|
|
|
|
Yes |
The name of the cookie to retrieve |
Returns
|
Type |
Description |
|---|---|
|
|
The cookie value if found |
|
|
If the cookie does not exist |
Example
// Get a cookie value
var token = tp.cookie.get("session_token");
if (token) {
console.log("Token found:", token);
} else {
console.log("No token set");
}
// Parse JSON stored in a cookie
var prefs = tp.cookie.get("preferences");
if (prefs) {
var preferences = JSON.parse(prefs);
console.log("Theme:", preferences.theme);
}
tp.cookie.remove(name, options)
Removes a cookie by name. Returns whether the cookie existed and was removed.
Parameters
|
Parameter |
Type |
Required |
Description |
|---|---|---|---|
|
|
|
Yes |
The name of the cookie to remove |
|
|
|
No |
Must match the |
Options Object
|
Option |
Type |
Description |
|---|---|---|
|
|
|
Path used when the cookie was set |
|
|
|
Domain used when the cookie was set |
Returns
|
Type |
Description |
|---|---|
|
|
Cookie existed and was removed |
|
|
Cookie did not exist |
Example
// Remove a simple cookie
var wasRemoved = tp.cookie.remove("username");
console.log("Cookie removed:", wasRemoved);
// Remove a cookie with specific path/domain
tp.cookie.remove("session_token", {
path: "/",
domain: ".example.com"
});
To successfully remove a cookie, the path and domain options must match those used when the cookie was originally set.
Complete Example
// Set a user session cookie
tp.cookie.set("user_session", "sess_abc123", {
path: "/",
expires: 7,
secure: true,
samesite: "strict"
});
// Later, retrieve it
var session = tp.cookie.get("user_session");
if (session) {
console.log("Active session:", session);
}
// On logout, remove it
if (tp.cookie.remove("user_session", { path: "/" })) {
console.log("Session cleared");
}
Notes
-
Cookies set without an
expiresoption are session cookies and will be deleted when the browser closes. -
The
secureoption should be used for sensitive data; these cookies are only sent over HTTPS. -
When
samesiteis set to"none", thesecureoption must also betrue. -
The
httpOnlyflag cannot be set via JavaScript; it is only available for server-set cookies.