I am trying to make the Enlarge function persistent using cookies and javascript.
I have modified the body statement in the dbtech_gallery template to:
<body onload="DBTech_Gallery.check_enlarge();">
I have also modified/added some code to gallery.js
The code correctly updates the cookie and the Enlarge toggles as expected.
The problem I am having is that the "onload" in the body statement, doesn't
seem to toggle it to the correct state when a new page is loaded if the cookie
is set. I am not getting any errors though. I thought that someone with more
javascript experience might be able to tell me what I am doing wrong or why
it doesn't work correctly.
Thank you for reading!
Rick
I have modified the body statement in the dbtech_gallery template to:
<body onload="DBTech_Gallery.check_enlarge();">
I have also modified/added some code to gallery.js
Code:
this.toggle_enlarge=function(small_w,large_w)
{
if(fetch_object('gallery_enlarge_toggle').checked)
{
createCookie('fsd_enlarge', 'true', '365');
document.getElementById("full_image").style.width=large_w+"px";
document.getElementById("gallery_enlarge_toggle").checked = true;
}
else
{
createCookie('fsd_enlarge', 'false', '365');
document.getElementById("full_image").style.width=small_w+"px";
document.getElementById("gallery_enlarge_toggle").checked = false;
}
};
this.check_enlarge=function()
{
var enlarge = readCookie('fsd_enlarge');
if (enlarge === 'true')
{
document.getElementById("full_image").style.width=large_w+"px";
document.getElementById("gallery_enlarge_toggle").checked = true;
}
else
{
document.getElementById("full_image").style.width=small_w+"px";
document.getElementById("gallery_enlarge_toggle").checked = false;
}
};
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 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;
}
The code correctly updates the cookie and the Enlarge toggles as expected.
The problem I am having is that the "onload" in the body statement, doesn't
seem to toggle it to the correct state when a new page is loaded if the cookie
is set. I am not getting any errors though. I thought that someone with more
javascript experience might be able to tell me what I am doing wrong or why
it doesn't work correctly.
Thank you for reading!
Rick
Upvote
0