Function generateCookie

  • Generates cookie string

    Parameters

    • name: string

      cookie name

    • value: string

      cookie value

    • Optional options: {
          domain?: string;
          expires?: number;
          path?: string;
          sameSite?: "none" | "strict" | "lax";
          secure?: boolean;
      }
      • Optional domain?: string

        A String indicating a valid domain where the cookie should be visible. The cookie will also be visible to all subdomains.

        Default

        undefined Cookie is visible only to the domain or subdomain of the page where the cookie was created
        
      • Optional expires?: number

        Expire cookie after x days (negative to remove cookie)

        Default

        undefined Cookie is removed when the user closes the browser
        
      • Optional path?: string

        A String indicating the path where the cookie is visible.

        Default

        undefined
        
      • Optional sameSite?: "none" | "strict" | "lax"

        A String, allowing to control whether the browser is sending a cookie along with cross-site requests.

        Default

        undefined No SameSite attribute set
        
      • Optional secure?: boolean

        A Boolean indicating if the cookie transmission requires a secure protocol (https).

        Default

        undefined No secure protocol requirement
        

    Returns string

    Example

    // Create a cookie, valid across the entire site:
    document.cookie = generateCookie('name', 'value')

    // Create a cookie that expires 7 days from now, valid across the entire site:
    document.cookie = generateCookie('name', 'value', { expires: 7 })

    // Delete a cookie by setting the expires parameter to zero:
    document.cookie = generateCookie('name', 'value', { expires: 0 })