1: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2: <html xmlns="http://www.w3.org/1999/xhtml">
3: <head>
4: <title>Local Storage</title>
5: <script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.5.1.js" type="text/javascript"></script>
6: </head>
7:
8: <body>
9: <div id="resultMessage"> </div>
10: <table style="position: absolute; left: 10px;">
11: <tr>
12: <td>
13: Firstname:
14: </td>
15: <td><input type="text" id="firstname" />
16: </td>
17: </tr>
18: <tr>
19: <td>
20: Lastname:
21: </td>
22: <td><input type="text" id="lastname" />
23: </td>
24: </tr>
25: <tr>
26: <td>
27: E-mail:
28: </td>
29: <td><input type="text" id="email" />
30: </td>
31: </tr>
32: <tr>
33: <td>
34: <input type="button" id="save" value="Create" />
35: </td>
36: <td>
37: <input type="button" id="print" value="Print"/>
38: </td>
39: </tr>
40: </table>
41: <script type="text/javascript">
42: $(function () {
43: if (!can_use_html5()) {
44: alert("This browser does not support Html5 storage, upgrade or change your browser!");
45: }
46: });
47:
48: $(function () {
49: $("#save").click(function () {
50:
51: var person = getPerson();
52:
53: //simple validation
54: if (person == null) {
55: alert("Fill in all fields please!");
56: return;
57: }
58:
59: var jsData = JSON.stringify(person);
60: localStorage.setItem($("#email").val(), jsData);
61:
62: $("#resultMessage").html($("#email").val() + " stored in local storage");
63: clearAllText();
64: })
65: });
66:
67: $(function () {
68: $("#print").click(function () {
69: for (var i = 0; i < window.localStorage.length; i++) {
70: document.write('<div id="record_' + i + '"> ' + localStorage[localStorage.key(i)] + '</div>');
71: }
72: localStorage.clear();
73: })
74: });
75:
76: function getPerson() {
77: var firstname = $("#firstname").val();
78: var lastname = $("#lastname").val();
79: var email = $("#email").val();
80: //simple validation
81: return (firstname == "" || lastname == "" || email == "") ? null : { Firstname: firstname, Lastname: lastname, Email: email };
82: }
83:
84: function can_use_html5() {
85: try {
86: return 'localStorage' in window && window['localStorage'] !== null;
87: }
88: catch (e) {
89: return false;
90: }
91: }
92:
93: function clearAllText() {
94:
95: $("#firstname").attr("value", "");
96: $("#lastname").attr("value", "");
97: $("#email").attr("value", "");
98: }
99: </Script>
100: </body>
101: </html>