Function generateCookie

  • Generates cookie string

    Parameters

    • name: string

      cookie name

    • value: string

      cookie value

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

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

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

        Expire cookie after x days (negative to remove cookie)

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

        A String indicating the path where the cookie is visible.

        undefined
        
      • OptionalsameSite?: "none" | "strict" | "lax"

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

        undefined No SameSite attribute set
        
      • Optionalsecure?: boolean

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

        undefined No secure protocol requirement
        

    Returns string

    // 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 })