Skip to content

Instantly share code, notes, and snippets.

@mattetti
Created July 9, 2011 18:35
Show Gist options
  • Select an option

  • Save mattetti/1073832 to your computer and use it in GitHub Desktop.

Select an option

Save mattetti/1073832 to your computer and use it in GitHub Desktop.
example of handling cookie data with full page cache
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[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;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else
var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function flashNotice(){
var notice = readCookie('flash_notice')
if(notice){
var unescaped = unescape(notice).replace(/\+/g,' ');
eraseCookie('flash_notice')
$('flash_notice').update(unescaped);
$('flash_notice').show();
} else {
var error = readCookie('flash_error')
if(error){
var unescaped = unescape(error).replace(/\+/g,' ');
eraseCookie('flash_error')
$('flash_error').update(unescaped);
$('flash_error').show();
}
}
return true;
}
function handleCachedUserLinks() {
var username = readCookie('username')
var loginLink = $('login')
var logout = $('logout')
var myRecentGames = $('myRecentGames')
var userGreetings = $('loginUsername')
if (username == null){
loginLink.show();
logout.hide();
if(myRecentGames){ myRecentGames.hide()};
}else{
// user is logged in and we have her username
loginLink.hide();
if(userGreetings){ userGreetings.update("<strong>Welcome " + username + '!</strong>'); }
gamer_menu_links(username);
gamer_card_links(username);
logout.show();
if(myRecentGames){ myRecentGames.show()};
};
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment