cookie vs. localstorage
cookie vs. localstorage
kevujin
Posts: 4Questions: 0Answers: 0
Hello Allan,
I look into code and I see, methods loading localstorage data
https://github.com/DataTables/DataTables/blob/master/media/src/model/model.defaults.js#L1141
and saving
https://github.com/DataTables/DataTables/blob/master/media/src/model/model.defaults.js#L1247
I would like to know if these methods are currently running while datatables are loaded. Because I have just done some tests and localstorage is empty -> state is sent in cookies (I deleted cookies and tried again and still the same result).
I could implement it by myself in config but I would like to be able to fallback to cookies if browser is not supporting localstorage. How to do this? I assume I will call some built in function in datatables that does this.
I am using firefox 20.0.1 and minified version of datatables 1.9.0.
Thanks for your answer.
kevujin
I look into code and I see, methods loading localstorage data
https://github.com/DataTables/DataTables/blob/master/media/src/model/model.defaults.js#L1141
and saving
https://github.com/DataTables/DataTables/blob/master/media/src/model/model.defaults.js#L1247
I would like to know if these methods are currently running while datatables are loaded. Because I have just done some tests and localstorage is empty -> state is sent in cookies (I deleted cookies and tried again and still the same result).
I could implement it by myself in config but I would like to be able to fallback to cookies if browser is not supporting localstorage. How to do this? I assume I will call some built in function in datatables that does this.
I am using firefox 20.0.1 and minified version of datatables 1.9.0.
Thanks for your answer.
kevujin
This discussion has been closed.
Replies
To implement localStorage and any other storage mechanism there are a number of callback methods available. See this blog post for more: http://datatables.net/blog/localStorage_for_state_saving
Allan
I read link you posted for sure. I would like to fallback to cookie if browser does not support local storage.
You stated here, that you can fallback to native cookie functions
http://datatables.net/forums/discussion/comment/30324#Comment_30324
How to do this in code?
Thanks
Kevujin
[code]
$(document).ready(function() {
$('#example').dataTable( {
"bStateSave": true,
"fnStateSave": function (oSettings, oData) {
try {
localStorage.setItem( 'DataTables_'+window.location.pathname, JSON.stringify(oData) );
} catch (e) {
// here goes code to use cookies
}
},
"fnStateLoad": function (oSettings) {
try {
return JSON.parse( localStorage.getItem('DataTables_'+window.location.pathname) );
} catch (e) {
// here goes code to use cookies
}
}
} );
} );
[/code]
Allan
Thanks for your support and for awesome product.
Kevujin