cookie vs. localstorage

cookie vs. localstorage

kevujinkevujin Posts: 4Questions: 0Answers: 0
edited April 2013 in DataTables 1.9
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

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    DataTables 1.9.4- use cookies and not local storage. This has been changed in the development version of DataTables and v1.10.0 will use localStorage by default rather than cookies.

    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
  • kevujinkevujin Posts: 4Questions: 0Answers: 0
    Thank you for your answer, so it is clear now to me.

    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
  • kevujinkevujin Posts: 4Questions: 0Answers: 0
    So I mean, what to put into catch
    [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]
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Currently you'd need to add the code to do the cookies. If you override the state storage, there isn't a way of also using the internal DataTables cookie function (actually, I think it probably is possible, but it is very hacky). You might be best using a cookie library, or even better, one which abstracts out the difference between localStorage and cookies.

    Allan
  • kevujinkevujin Posts: 4Questions: 0Answers: 0
    Ok, I thought there is some cookie job function. Never mind then. I'll detect localStorage beforehand and if present, I'll use it.

    Thanks for your support and for awesome product.

    Kevujin
This discussion has been closed.