The mcrSf.cookieGet() method requires one argument and returns a string value.
The required argument defines the cookie name. There is no option to specify the path that the cookie belongs to. A path was specified at the time of setting the cookie and cookies can only be accessed from files and subfolders within this path.
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. |
Return Value
Type | Description |
---|---|
String | The stored value of the cookie. |
undefined | The method will return undefined if the requested cookie can not be read. |
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 cookieGet(cookieName) {
cookieName += "=";
var cookieArray = decodeURIComponent(document.cookie).split(';');
for(var i = 0; i < cookieArray.length; i++) {
var cookieEl = cookieArray[i].replace(/^\s+|\s+$/gm,'');
if (cookieEl.substring(0, cookieName.length) == cookieName) {
return cookieEl.substring(cookieName.length, cookieEl.length);
}
}
return undefined;
}
The mcrSf
Library Method
/**
* Get (read) a cookie
* @version 0.1
* @param {String} cookieName - the name of the cookie
* @return {String} - value of cookie
*
* STRATEGY: Read a value from the cookie property from the document object
*/
cookieGet: function(cookieName) {
cookieName += "=";
var cookieArray = decodeURIComponent(document.cookie).split(';');
for(var i = 0; i < cookieArray.length; i++) {
var cookieEl = cookieArray[i].replace(/^\s+|\s+$/gm,'');
if (cookieEl.substring(0, cookieName.length) == cookieName) {
return cookieEl.substring(cookieName.length, cookieEl.length);
}
}
return undefined;
}
Version History
Revision Date | Details |
---|---|
0.1 05-Aug-17 | First release, cookieGet() . |