calendarxp Site Admin
Joined: 30 Jan 2005 Posts: 409
|
Posted: Sat Mar 19, 2005 9:06 pm Post subject: [FlatCalendarXP]How to keep the date selected across different pages? |
|
|
Although you could use server-side script to retrieve the selected date via form postback and restore it in the page onload event, using cookie is just a much easier solution - simply add the following code to the plugins.js:
Code: | function fOnChange(y,m,d,e) {
if (d!=0) fCreateCookie(_cookieName, y+'-'+m+'-'+d);
// ... other code, if any ...
}
var _cookieName="selectedDate";
var _sd=fReadCookie(_cookieName);
if (_sd) {
_sd=_sd.split("-");
gdSelect=[_sd[0],_sd[1],_sd[2]];
gCurMonth=[_sd[0],_sd[1]];
}
function fCreateCookie(name,value)
{
document.cookie = name+"="+value+"; path=/";
}
function fReadCookie(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 mini calendar beneath the left navigation panel on calendarxp.net is a good example.
NOTE:
- Do NOT use the fAfterSelected plugin to create cookie, because fAfterSelected may not get called if the event action of the selected date redirects the browser to another place.
- The above code may not work with certain plugin - e.g. multi-select plugin will disable the regular fSetDate() call which in turn won't trigger the fOnChange() when a date is selected. In order to make it work with the MultiPickerDemo, you will have to append the following code to the end (must be placed after the multi-select plugin code) of plugins.js:
Code: | var _cookieName="selectedDate";
var _cookieStr=fReadCookie(_cookieName);
var _cds=(_cookieStr&&_cookieStr.length>0)?eval(_cookieStr):[];
function fCreateCookie(name,value)
{
var valueStr="[";
for (var i=0;i<value.length;i++)
valueStr+=(i>0?",":"")+"["+value[i][0]+","+value[i][1]+"]";
valueStr+="]";
document.cookie = name+"="+valueStr+"; path=/";
}
function fReadCookie(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;
} | Then, prepend some code to the fOnload() in plugins.js so that it looks like following:
Code: | function fOnload() {
// -- append code start --
for (var i=0; i<_cds.length; i++) {
var _dt0=new Date(_cds[i][0]), _dt1=new Date(_cds[i][1]);
fAddRange(_dt0.getUTCFullYear(),_dt0.getUTCMonth()+1,_dt0.getUTCDate(),false);
fAddRange(_dt1.getUTCFullYear(),_dt1.getUTCMonth()+1,_dt1.getUTCDate(),true);
}
// -- append code end --
if (gContainer.fInitAgenda) gContainer.fInitAgenda();
} | Finally, append the following line to the bottom (last line in function) of both fAddRange() and fRemoveRange(): Code: | fCreateCookie(_cookieName,_pds); |
_________________ Copyright 2003-2011 Idemfactor Solutions, Inc. All rights reserved. |
|