Skip to content

Instantly share code, notes, and snippets.

@aslamdoctor
Created February 20, 2024 16:30
Show Gist options
  • Select an option

  • Save aslamdoctor/7d66d566f05a018eb87d63ad0c9a7a22 to your computer and use it in GitHub Desktop.

Select an option

Save aslamdoctor/7d66d566f05a018eb87d63ad0c9a7a22 to your computer and use it in GitHub Desktop.

Revisions

  1. aslamdoctor created this gist Feb 20, 2024.
    48 changes: 48 additions & 0 deletions cookieHelper.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    // Cookie helper functions
    const CookieHelper = {
    /**
    * Set Cookie.
    *
    * @param {string} name
    * @param {string} value
    * @param {string} minutes
    */
    setCookie( name, value, minutes ) {
    let expires = '';

    if ( minutes ) {
    const now = new Date();
    expires = '; expires=' + new Date(now.getTime() + minutes * 60000);
    }

    document.cookie = name + '=' + ( value || '' ) + expires + ';';
    },

    /**
    * Get Cookie.
    *
    * @param {string} name
    * @return {string} null
    */
    getCookie( name ) {
    const cookies = document.cookie.split( ';' );

    for ( const cookie of cookies ) {
    if ( -1 < cookie.indexOf( name + '=' ) ) {
    return cookie.split( '=' )[ 1 ];
    }
    }

    return null;
    },

    /**
    * Delete Cookie.
    *
    * @param {string} name
    * @return {string} null
    */
    deleteCookie( name ) {
    document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
    },
    };