Skip to content

Commit e5cafe4

Browse files
Jay Bryantfmbenhassine
authored andcommitted
Use local storage rather than a cookie
I learnt that local storage is the preferred way to save a user's preferences. A cookie is sent to the server on every request, which makes for needless traffic. I "borrowed" the idea from the Spring Framework docs.
1 parent 9191a72 commit e5cafe4

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

spring-batch-docs/asciidoc/js/DocumentToggle.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
$(document).ready(function(){
22

3+
var BATCH_LANGUAGES = ["java", "xml", "both"];
34
var $xmlButton = $("#xmlButton");
45
var $javaButton = $("#javaButton");
56
var $bothButton = $("#bothButton");
@@ -10,20 +11,24 @@ $(document).ready(function(){
1011
var $javaContent = $("*.javaContent");
1112
var $javaContentAll = $("*.javaContent > *");
1213

13-
setJava();
14-
15-
// Initial cookie handler. This part remembers the reader's choice and sets the toggle
16-
// accordingly.
17-
var docToggleCookieString = Cookies.get("docToggle");
18-
if (docToggleCookieString != null) {
19-
if (docToggleCookieString === "xml") {
14+
// Initial cookie handler. This part remembers the
15+
// reader's choice and sets the toggle accordingly.
16+
var lang = window.localStorage.getItem("docToggle");
17+
if (BATCH_LANGUAGES.indexOf(lang) === -1) {
18+
lang = "java";
19+
$javaButton.prop("checked", true);
20+
setJava();
21+
} else {
22+
if (lang === "xml") {
2023
$xmlButton.prop("checked", true);
2124
setXml();
22-
} else if (docToggleCookieString === "java") {
25+
}
26+
if (lang === "java") {
2327
$javaButton.prop("checked", true);
2428
setJava();
25-
} else if (docToggleCookieString === "both") {
26-
$bothButton.prop("checked", true);
29+
}
30+
if (lang === "both") {
31+
$javaButton.prop("checked", true);
2732
setBoth();
2833
}
2934
}
@@ -48,7 +53,7 @@ $(document).ready(function(){
4853
$xmlContentAll.removeClass("js-toc-ignore");
4954
window.dispatchEvent(new Event("tocRefresh"));
5055
tocbot.refresh();
51-
Cookies.set('docToggle', 'xml', { expires: 3652 });
56+
window.localStorage.setItem('docToggle', 'xml');
5257
}
5358

5459
function setJava() {
@@ -58,7 +63,7 @@ $(document).ready(function(){
5863
$javaContentAll.removeClass("js-toc-ignore");
5964
window.dispatchEvent(new Event("tocRefresh"));
6065
tocbot.refresh();
61-
Cookies.set('docToggle', 'java', { expires: 3652 });
66+
window.localStorage.setItem('docToggle', 'java');
6267
}
6368

6469
function setBoth() {
@@ -68,7 +73,7 @@ $(document).ready(function(){
6873
$xmlContentAll.removeClass("js-toc-ignore");
6974
window.dispatchEvent(new Event("tocRefresh"));
7075
tocbot.refresh();
71-
Cookies.set('docToggle', 'both', { expires: 3652 });
76+
window.localStorage.setItem('docToggle', 'both');
7277
}
7378

7479
});

0 commit comments

Comments
 (0)