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
Parameter | Type | Description |
---|---|---|
cookieName | String | The name of the cookie. |
cookieValue | String | The value of the cookie. |
cookieLife (Optional) | Number | The life of the cookie in number of days. Optional, default value of null . |
cookiePath (Optional) | String | The path of the cookie. Optional, default value of '/' . |
Return Value
Type | Description |
---|---|
undefined | The 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() . |