Use same cookie for different URLs/paths
Use same cookie for different URLs/paths
Hello
First of all: Allan, thank you very much for DataTables. It is a very nice library, extending jQuery very usefull!
According to the two discussions (http://datatables.net/forums/comments.php?DiscussionID=202, http://datatables.net/forums/comments.php?DiscussionID=3108&page=1) I have to following problem:
I use DataTables with Drupal and I have the same table across different pages, i.e. different URLs. I want, that the table state is saved for every table on every page in the same cookie.
With oSettings.fnCookieCallback it is possible to give every cookie the same name with the same path before the cookie is saved, e.g.:
[code]
'fnCookieCallback': function (sName, oData, sExpires, sPath) {
return 'my_cookie_name' + '=' + JSON.stringify(oData) + '; expires=' + sExpires + '; path=/';
},
[/code]
But if I return to the page, where I saved the cookie before, _fnReadCookie will not find the proper cookie. So, I changed your code in the following way:
1. Next to "this.fnCookieCallback = null;" I introduced a new oSettings variable:
[code]
this.fnCookieReadCallback = null;
[/code]
2. After "_fnMap( oSettings, oInit, "fnCookieCallback");" I inserted:
[code]
_fnMap( oSettings, oInit, "fnCookieReadCallback");
[/code]
3. In "_fnLoadState" I changed initialisation of variable "sData" and added previously introduced variable to the function call:
[code]
var sData = _fnReadCookie( oSettings.sCookiePrefix+o.Settings.sInstance, oSettings.fnCookieReadCallback );
[/code]
4. In "_fnReadCookie" I changed the functions paramter to:
[code]
function _fnReadCookie ( sName, fnCallback )
[/code]
5. In "_fnReadCookie" after the initialization of the variables "aParts", "sNameEQ" and "sCookieContents" I added the following:
[code]
if ( fnCallback !== null && fnCallback !== undefined )
{
sNameEQ = fnCallback( sName, aParts ) + '=';
}
[/code]
So, now in the DataTable initialisation I put the new variable like:
[code]
'fnCookieReadCallback': function ( sName, aParts ) {
return 'my_cookie_name';
},
[/code]
I know, actually for my purpose, I the parameters are useless. But maybe in a situation in the future, I need it.
I guess, there is a much more elegant way to do this, but I didn't want to change too much of your code, because I do not want to change all the things, if I'll update to a newer version of DataTables.
But maybe, I'm completly wrong, and my problem is already solved in v1.7.6?
Thanks a lot,
Marco
First of all: Allan, thank you very much for DataTables. It is a very nice library, extending jQuery very usefull!
According to the two discussions (http://datatables.net/forums/comments.php?DiscussionID=202, http://datatables.net/forums/comments.php?DiscussionID=3108&page=1) I have to following problem:
I use DataTables with Drupal and I have the same table across different pages, i.e. different URLs. I want, that the table state is saved for every table on every page in the same cookie.
With oSettings.fnCookieCallback it is possible to give every cookie the same name with the same path before the cookie is saved, e.g.:
[code]
'fnCookieCallback': function (sName, oData, sExpires, sPath) {
return 'my_cookie_name' + '=' + JSON.stringify(oData) + '; expires=' + sExpires + '; path=/';
},
[/code]
But if I return to the page, where I saved the cookie before, _fnReadCookie will not find the proper cookie. So, I changed your code in the following way:
1. Next to "this.fnCookieCallback = null;" I introduced a new oSettings variable:
[code]
this.fnCookieReadCallback = null;
[/code]
2. After "_fnMap( oSettings, oInit, "fnCookieCallback");" I inserted:
[code]
_fnMap( oSettings, oInit, "fnCookieReadCallback");
[/code]
3. In "_fnLoadState" I changed initialisation of variable "sData" and added previously introduced variable to the function call:
[code]
var sData = _fnReadCookie( oSettings.sCookiePrefix+o.Settings.sInstance, oSettings.fnCookieReadCallback );
[/code]
4. In "_fnReadCookie" I changed the functions paramter to:
[code]
function _fnReadCookie ( sName, fnCallback )
[/code]
5. In "_fnReadCookie" after the initialization of the variables "aParts", "sNameEQ" and "sCookieContents" I added the following:
[code]
if ( fnCallback !== null && fnCallback !== undefined )
{
sNameEQ = fnCallback( sName, aParts ) + '=';
}
[/code]
So, now in the DataTable initialisation I put the new variable like:
[code]
'fnCookieReadCallback': function ( sName, aParts ) {
return 'my_cookie_name';
},
[/code]
I know, actually for my purpose, I the parameters are useless. But maybe in a situation in the future, I need it.
I guess, there is a much more elegant way to do this, but I didn't want to change too much of your code, because I do not want to change all the things, if I'll update to a newer version of DataTables.
But maybe, I'm completly wrong, and my problem is already solved in v1.7.6?
Thanks a lot,
Marco
This discussion has been closed.