Member Login:


Set (store/save) a cookie with JavaScript

Document Reference: TN201708001 - Rev: 4.02 - Last Update: 20-08-2017 21:49 GMT - Downloaded: 24-Jul-2019 08:28 GMT

The mcrSf.cookieSet() method requires a minimum of two arguments and has an undefined return value.

The first argument defines the cookie name and the second argument takes the cookie value. The first optional argument sets the cookie life in days and the second optional argument can be supplied to define the path that the cookie will belong to.

Please Note: Due to security issues and other problems, Google Chrome and other popular browsers have disabled storing cookies for local files. Cookies can only be stored and retrieved for files that are served from a webserver.

Parameters

ParameterTypeDescription
cookieNameStringThe name of the cookie.
cookieValueStringThe value of the cookie.
cookieLife
(Optional)
NumberThe life of the cookie in number of days.
Optional, default value of null.
cookiePath
(Optional)
StringThe path of the cookie.
Optional, default value of '/'.

Return Value

TypeDescription
undefinedThe method has no return value.

The Stand-Alone Function

Below code is ready for copy/paste and will work just fine on it's own, without linking the mcrSf library file.

 function cookieSet(cookieName, cookieValue, cookieLife, cookiePath) {
    cookieLife = cookieLife || null;
    var cookieString = cookieName + "=" + encodeURIComponent(cookieValue);
    if (cookieLife > 0) {
        var timeStamp = new Date();
        timeStamp.setTime(timeStamp.getTime() + (cookieLife*24*60*60*1000));
        cookieString += "; expires=" + timeStamp.toUTCString();
    }
    if (cookiePath !== "") {
        cookiePath = cookiePath || "/";
        cookieString += "; path=" + cookiePath;
    }
    document.cookie = cookieString;
}

The mcrSf Library Method

 /**
* Set (create/store) a cookie
* @version 0.1
* @param {String} cookieName - the name of the cookie
* @param {String} cookieValue - the value of the cookie
* @param {Number} [cookieLife=null] - the life of the cookie in number of days
* @param {String} [cookiePath="/"] - the path of the cookie (default: "path=/")
* @return {undefined} - method has no return value
*
* STRATEGY: Assign a value to the cookie property from the document object
*/
cookieSet: function(cookieName, cookieValue, cookieLife, cookiePath) {
    cookieLife = cookieLife || null;
    var cookieString = cookieName + "=" + encodeURIComponent(cookieValue);
    if (cookieLife > 0) {
        var timeStamp = new Date();
        timeStamp.setTime(timeStamp.getTime() + (cookieLife*24*60*60*1000));
        cookieString += "; expires=" + timeStamp.toUTCString();
    }
    if (cookiePath !== "") {
        cookiePath = cookiePath || "/";
        cookieString += "; path=" + cookiePath;
    }
    document.cookie = cookieString;
}

Version History

Revision
Date
Details
0.1
05-Aug-17
First release, cookieSet().

This website uses cookies to give you the best experience on our website, to personalise content and to analyse our website traffic. Some cookies may have been set already. To find out more about our use of cookies you can visit our Privacy Statement. By browsing this website, you agree to our use of cookies.

Hide this message