-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathweb-storage-demo-ui-app-full.html
111 lines (87 loc) · 3.69 KB
/
web-storage-demo-ui-app-full.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<!DOCTYPE html>
<html>
<head>
<!-- (c) javascript-html5-tutorial.com -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>JavaScript localStorage - demo app</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<style>
#main {
border: solid 1px #00e;
margin: 0 auto;
padding: 5px;
text-align: center;
width: 33%;
}
</style>
</head>
<body>
<section id="main">
<form onsubmit="javascript:storeSettings()">
<label>Select font size: </label><br />
<input id="your_font" type="number" max="50" min="5" value="14">
<br />
<label>Select border size: </label><br />
<input id="your_border" type="number" max="10" min="1" value="1">
<br />
<label>Select color for background: </label><br />
<input id="your_color" type="color" value="#ffffff">
<br />
<p>
<input type="submit" value="Save settings">
<input onclick="removeSettings()" type="reset" value="Remove settings">
</p>
</form>
</section>
<script>
function storeSettings() {
if ('localStorage' in window && window['localStorage'] !== null) {
try {
var your_color = $('#your_color').val();
var your_font = $('#your_font').val();
var your_border = $('#your_border').val();
localStorage.setItem('bgcolor', your_color);
localStorage.setItem('fontsize', your_font);
localStorage.setItem('border', your_border);
} catch (e) {
alert('An error occured');
}
} else {
alert('Sorry, your browser must have localStorage support');
}
}
function setDefaults() {
document.body.style.backgroundColor = '#ffffff';
document.body.style.fontSize = '14px';
$('#main').css('border', 'solid 1px #00e');
$('#your_color').val('#ffffff');
$('#your_font').val('14');
$('#your_border').val('1');
}
function removeSettings() {
localStorage.removeItem('bgcolor');
localStorage.removeItem('fontsize');
localStorage.removeItem('border');
setDefaults();
}
function useSettings() {
if (localStorage.length != 0) {
document.body.style.backgroundColor = localStorage.getItem('bgcolor');
var font_size = localStorage.getItem('fontsize');
document.body.style.fontSize = font_size + 'px';
var border_size = localStorage.getItem('border');
$('#main').css('border', 'solid ' + border_size + 'px #00e');
$('#your_color').val(localStorage.getItem('bgcolor'));
$('#your_font').val(font_size);
$('#your_border').val(border_size);
} else {
setDefaults();
}
}
$( document ).ready(function() {
useSettings()
});
</script>
</body>
</html>