Primefaces – save current page gallery in session cookie


Using a session cookie to save and reload current page Gallery after page refresh:

JavaScript:

var currPage;
var Cookies = {
init: function () {
var allCookies = document.cookie.split(‘; ‘);
for (var i=0;i<allCookies.length;i++) {
var cookiePair = allCookies[i].split(‘=’);
this[cookiePair[0]] = cookiePair[1];
}
},
create: function (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=/GiftCoupon/”;
this[name] = value;
},
erase: function (name) {
this.create(name,”,-1);
this[name] = undefined;
}
};
Cookies.init();
function savePage() {
currPage = carouselVar.cfg.page;
Cookies.erase(‘gallery’);
Cookies.create(‘gallery’, currPage);
}
function setPage() {
carouselVar.cfg.effect = ‘fade’;

carouselVar.setPage(currPage);
carouselVar.cfg.effect = ‘slide’;
Cookies.erase(‘gallery’);
Cookies.create(‘gallery’, currPage);
}
$(document).ready(function() {
var i, ARRcookies = document.cookie.split(“;”);
for (i=0;i<ARRcookies.length;i++) {

if(ARRcookies[0].split(“=”)[0]==’gallery’) {
carouselVar.cfg.effect = ‘fade’;
carouselVar.setPage(ARRcookies[0].split(“=”)[1]);
carouselVar.cfg.effect = ‘slide’;
break;
}
}
});

 

xhtml

<p:commandLink id=”link” onclick=”savePage();” oncomplete=”setPage();”>

<p:graphicImage¬†value=”/images/galleria/#{image}”¬†¬† title=”#{image}”/>

</p:commandLink>

Leave a Reply

Your email address will not be published.