We’ve migrated our documentation to a new site, which means some URLs have changed.
Subscriptions

Working with the tp.cookie Object

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

name

string

Yes

The name of the cookie

value

string

Yes

The value to store

options

object

No

Configuration options (see below)

Options Object

Option

Type

Default

Description

path

string

"/"

URL path scope for the cookie

domain

string

Current domain

Domain scope for the cookie

expires

number or Date

Session

Expiration as days (number) or Date object

secure

boolean

false

Only transmit over HTTPS

samesite

boolean or string

"lax"

SameSite attribute: "strict", "lax", or "none"

priority

string

"medium"

Cookie priority: "low", "medium", or "high"

raw

boolean

false

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

name

string

Yes

The name of the cookie to retrieve

Returns

Type

Description

string

The cookie value if found

undefined

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

name

string

Yes

The name of the cookie to remove

options

object

No

Must match the path and domain used when setting

Options Object

Option

Type

Description

path

string

Path used when the cookie was set

domain

string

Domain used when the cookie was set

Returns

Type

Description

true

Cookie existed and was removed

false

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 expires option are session cookies and will be deleted when the browser closes.

  • The secure option should be used for sensitive data; these cookies are only sent over HTTPS.

  • When samesite is set to "none", the secure option must also be true.

  • The httpOnly flag cannot be set via JavaScript; it is only available for server-set cookies.

Last updated: