The following counts the number of page loads and then does something if < MIN_PAGE_VIEWS
.
Can anyone give me any pointers to improve both this function and my JavaScript in general? Both in terms of patterns, design and algorithm.
// Call on body onload event.
// Checks to see if prompt is to be displayed and if banner is to be displayed.
function bodyOnLoad() {
var MIN_PAGE_VIEWS = 3;
if(readCookie('counter-cookie') == null){
createCookie('counter-cookie', '0', '150');
}
// Only start showing random popup after second page visit
if (readCookie('counter-cookie') < MIN_PAGE_VIEWS) {
var iCount = parseInt(readCookie('counter-cookie'));
iCount++;
createCookie('counter-cookie',iCount.toString(), '150');
}
else{
if (readCookie('ab5cd57e-871a-4b65') != 'yes') {
showPanel('popupPanel',80,240,'yes');
}
}
if(readCookie('banner-cookie') == 'yes'){
showPanel('surveyBanner',100,100, 'no');
displayBanner();
}
}
// New Cookie and Set expiration date
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=/";
}
// Separates cookies
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;
}