Skip to content

Instantly share code, notes, and snippets.

@aaronranard
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save aaronranard/11153423 to your computer and use it in GitHub Desktop.

Select an option

Save aaronranard/11153423 to your computer and use it in GitHub Desktop.
Javascript: Cookie Management
/**
* determine whether this is the first visit to the animation page
* @param {string} c_name cookie name
* @return {string} cookie value
*/
function getCookie(c_name){
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start === -1){
c_start = c_value.indexOf(c_name + "=");
}
if (c_start === -1){
c_value = null;
}
else{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end === -1){
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
/**
* Sets a cookie determining action for the next animation view
* @param {string} c_name cookie name
* @param {string} value the value to be set
* @param {int} exdays days until expiry
*/
function setCookie(c_name,value,exdays){
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays===null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
/**
* Determine whether the animation cookie has been set
* @return {boolean} false if cookie exists, true if cookie was set and animation should continue
*/
function checkCookie(){
var username=getCookie("played");
if (username!==null && username!=="")
{
// Do not play animation
return false;
}
else
{
// Play Animation
setCookie("played","true",365);
return true;
}
}
@aaronranard
Copy link
Copy Markdown
Author

Updated for JSLinting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment