From b2aebce382809ab249d3437466a89fd33d951109 Mon Sep 17 00:00:00 2001 From: Jaume Nicolau Date: Wed, 21 Nov 2018 12:02:51 +0100 Subject: [PATCH] Add location.hash to local storage state key Append the current hash to the key of the local storage save state. --- js/model/model.defaults.js | 4 ++-- old-tests/unit_test.js | 2 +- test/options/Options/stateDuration.js | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/js/model/model.defaults.js b/js/model/model.defaults.js index 4b0b3f7cc..e2bb115fc 100644 --- a/js/model/model.defaults.js +++ b/js/model/model.defaults.js @@ -1104,7 +1104,7 @@ DataTable.defaults = { try { return JSON.parse( (settings.iStateDuration === -1 ? sessionStorage : localStorage).getItem( - 'DataTables_'+settings.sInstance+'_'+location.pathname + 'DataTables_'+settings.sInstance+'_'+location.pathname+location.hash ) ); } catch (e) {} @@ -1205,7 +1205,7 @@ DataTable.defaults = { "fnStateSaveCallback": function ( settings, data ) { try { (settings.iStateDuration === -1 ? sessionStorage : localStorage).setItem( - 'DataTables_'+settings.sInstance+'_'+location.pathname, + 'DataTables_'+settings.sInstance+'_'+location.pathname+location.hash, JSON.stringify( data ) ); } catch (e) {} diff --git a/old-tests/unit_test.js b/old-tests/unit_test.js index 5be730335..5da72e49b 100644 --- a/old-tests/unit_test.js +++ b/old-tests/unit_test.js @@ -114,7 +114,7 @@ var oTest = { { var s = oTable.fnSettings(); - localStorage.setItem( 'DataTables_'+s.sInstance+'_'+window.location.pathname, null ); + localStorage.setItem( 'DataTables_'+s.sInstance+'_'+window.location.pathname+window.location.hash, null ); }, diff --git a/test/options/Options/stateDuration.js b/test/options/Options/stateDuration.js index 47decfa86..052bda278 100644 --- a/test/options/Options/stateDuration.js +++ b/test/options/Options/stateDuration.js @@ -15,7 +15,7 @@ describe('stateDuration Option', function() { $('#example') .DataTable() .state.clear(); - expect(sessionStorage['DataTables_example_' + location.pathname]).toBeDefined(); + expect(sessionStorage['DataTables_example_' + location.pathname+location.hash]).toBeDefined(); }); dt.html('basic'); @@ -30,14 +30,14 @@ describe('stateDuration Option', function() { .DataTable() .state.clear(); - expect(localStorage['DataTables_example_' + location.pathname]).toBeDefined(); + expect(localStorage['DataTables_example_' + location.pathname+location.hash]).toBeDefined(); // The following test should fail - but it is passing due to // https://sprymedia.manuscript.com/f/cases/394/API-call-state-clear-isn-t-clearing-the-state // remove the test when that bug is fixed - it will cause the test to fail. // The second test is just a failsafe to ensure it is behaving, so could optionally be removed - expect(sessionStorage['DataTables_example_' + location.pathname]).toBeDefined(); - expect($.isEmptyObject($.parseJSON(sessionStorage['DataTables_example_' + location.pathname]))).toBe(true); + expect(sessionStorage['DataTables_example_' + location.pathname+location.hash]).toBeDefined(); + expect($.isEmptyObject($.parseJSON(sessionStorage['DataTables_example_' + location.pathname+location.hash]))).toBe(true); }); }); });