How do I create and read a value from cookie?

前端 未结 19 1610
梦如初夏
梦如初夏 2020-11-21 04:42

How can I create and read a value from a cookie in JavaScript?

19条回答
  •  有刺的猬
    2020-11-21 05:01

    You can use my cookie ES module for get/set/remove cookie.

    Usage:

    In your head tag, include the following code:

    
    

    or

    
    

    Now you can use window.cookie for store user information in web pages.

    cookie.isEnabled()

    Is the cookie enabled in your web browser?

    returns {boolean} true if cookie enabled.
    

    Example

    if ( cookie.isEnabled() )
        console.log('cookie is enabled on your browser');
    else
        console.error('cookie is disabled on your browser');
    

    cookie.set( name, value )

    Set a cookie.

    name: cookie name.
    value: cookie value.
    

    Example

    cookie.set('age', 25);
    

    cookie.get( name[, defaultValue] );

    get a cookie.

    name: cookie name.
    defaultValue: cookie default value. Default is undefined.
    returns cookie value or defaultValue if cookie was not found
    
    Example
    var age = cookie.get('age', 25);
    

    cookie.remove( name );

    Remove cookie.

    name: cookie name.
    
    Example
    cookie.remove( 'age' );
    

    Example of usage

提交回复
热议问题