Skip to content

Instantly share code, notes, and snippets.

@pavr0m
Last active January 30, 2019 21:07
Show Gist options
  • Save pavr0m/4c5fc649a26a4a647d5db6ead59a0d41 to your computer and use it in GitHub Desktop.
Save pavr0m/4c5fc649a26a4a647d5db6ead59a0d41 to your computer and use it in GitHub Desktop.
/**
* Simplest cookie operations
*/
var simpCookie = {
set: function( name, value, path = '', expires = '', domain = '' ) {
var host, domain, domainParts;
host = location.host;
// set cookie name and value
newCookie = name + '=' + value;
// set path if specified
if ( !( path == '' ) ) {
path = '; path=' + path;
newCookie = newCookie + path;
}
newCookie = newCookie + '; path=' + path;
// set expires if specified
if ( !( expires == '' ) ) {
var date = new Date();
date.setTime( date.getTime() + ( expires * 24 * 60 * 60 * 1000) );
expires = '; expires=' + date.toUTCString();
newCookie = newCookie + expires;
}
// set domain if specified
if ( !( domain == '' ) ) {
domain = '; domain=' + domain;
newCookie = newCookie + domain;
}
// set cookie
document.cookie = newCookie;
// check if cookie has been set, otherwise output error log message
if ( ( prCookie.get(name) == null ) || ( Cookie.get(name) != value ) ) {
console.log( 'prCookie: couldn\'t set cookie \"' + name + '"' );
}
}, // end set
get: function( name ) {
var name, allCookies;
name = name + '=';
allCookies = document.cookie.split( ';' );
for (var i=0; i < allCookies.length; i++) {
var c = allCookies[i];
while ( c.charAt(0) == ' ' ) {
c = c.substring( 1, c.length );
}
if ( c.indexOf( nameEQ ) == 0 ) return c.substring( nameEQ.length,c.length );
}
return null;
}, // end get
erase: function(name) {
simpCookie.set( name, '' );
} // end erase
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment