{"id":1615,"date":"2016-06-16T17:32:18","date_gmt":"2016-06-17T00:32:18","guid":{"rendered":"http:\/\/10kdev.net\/?p=1615"},"modified":"2016-06-16T12:44:37","modified_gmt":"2016-06-16T19:44:37","slug":"tooling-for-localstorage","status":"publish","type":"post","link":"http:\/\/10kdev.net\/?p=1615","title":{"rendered":"Tooling for localstorage"},"content":{"rendered":"<p><strong>Localstorage<\/strong> is a newer way to store data locally in a browser, kind of like a cookie.\u00a0 It is supposed to have a much easier interface than cookies and can do JSON.\u00a0 Another developer I know was using this and she was using localstorage to track local data\u00a0 for some dynamic web interactions.\u00a0 Nothing will get sent back to the server &#8212; you&#8217;d still have to use cookies for that (example &#8211; a JSESSIONID).\u00a0 But considering that much of the javascript I&#8217;ve written in my life was managing browser data by doing &#8220;single page websites&#8221; or passing form data with hidden fields back and forth, or passing data via query parameters &#8212; the localstorage idea is very compelling.<\/p>\n<p>I did a little searching and this <a href=\"http:\/\/htmldog.com\/guides\/javascript\/advanced\/localstorage\/\">succinct post on HTML Dog has a nice example<\/a>.<\/p>\n<p><strong>M<\/strong><strong>y interest for this post is in the tooling around localstorage<\/strong>.<\/p>\n<p>What I want to do is create a localstorage entry and then see it in a tool like Firebug or a web development tool much like a cookie.\u00a0 I&#8217;ve chosen Firefox built in web developer tools for now.<\/p>\n<p>First, I have to create some localstorage.\u00a0 I navigate to this blog site and open up Firefox&#8217;s\u00a0 developer and toggle the toolbar<\/p>\n<p><a href=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-16-at-2.01.43-PM.png\" rel=\"attachment wp-att-1616\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1616\" src=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-16-at-2.01.43-PM.png\" alt=\"Screen Shot 2016-06-16 at 2.01.43 PM\" width=\"195\" height=\"272\" \/><\/a><\/p>\n<p>Click on the Toolbox Options and then check Storage.<\/p>\n<p><a href=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-16-at-2.03.20-PM.png\" rel=\"attachment wp-att-1617\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1617\" src=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-16-at-2.03.20-PM.png\" alt=\"Screen Shot 2016-06-16 at 2.03.20 PM\" width=\"510\" height=\"208\" \/><\/a><\/p>\n<p>Create a localstorage value, I&#8217;ll just use HTML Dog&#8217;s JSON Example, and include a println to show I can try out the setter\/getter functionality.<\/p>\n<pre style=\"padding-left: 30px;\"><code class=\"javascript\">localStorage.setItem('user', JSON.stringify({\r\n\u00a0\u00a0\u00a0 username: 'htmldog',\r\n\u00a0\u00a0\u00a0 api_key: 'abc123xyz789'\r\n}));\r\nvar user = JSON.parse(localStorage.getItem('user'));\r\nprint(JSON.stringify(user));\r\n<\/code><\/pre>\n<p>Execute it in the console of the developer&#8217;s tool.<\/p>\n<p><a href=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-16-at-2.14.28-PM.png\" rel=\"attachment wp-att-1618\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1618\" src=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-16-at-2.14.28-PM.png\" alt=\"Screen Shot 2016-06-16 at 2.14.28 PM\" width=\"400\" height=\"211\" \/><\/a>And after.<\/p>\n<p><a href=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-16-at-2.16.06-PM.png\" rel=\"attachment wp-att-1620\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1620\" src=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-16-at-2.16.06-PM.png\" alt=\"Screen Shot 2016-06-16 at 2.16.06 PM\" width=\"402\" height=\"212\" srcset=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-16-at-2.16.06-PM.png 412w, http:\/\/10kdev.net\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-16-at-2.16.06-PM-300x159.png 300w\" sizes=\"(max-width: 402px) 100vw, 402px\" \/><\/a>We received the data back correctly for &#8220;user&#8221; object . . .<\/p>\n<p style=\"padding-left: 30px;\">&#8220;{&#8220;username&#8221;:&#8221;htmldog&#8221;,&#8221;api_key&#8221;:&#8221;abc123xyz789&#8243;}&#8221;<\/p>\n<p>Look in Storage.\u00a0 Wow!\u00a0 that&#8217;s a lot of cookies.<\/p>\n<p><a href=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-16-at-2.19.19-PM.png\" rel=\"attachment wp-att-1621\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1621\" src=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-16-at-2.19.19-PM.png\" alt=\"Screen Shot 2016-06-16 at 2.19.19 PM\" width=\"530\" height=\"141\" \/><\/a><\/p>\n<p>And here is the localstorage data for &#8220;user&#8221;:<\/p>\n<p><a href=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-16-at-2.20.30-PM.png\" rel=\"attachment wp-att-1622\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1622\" src=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-16-at-2.20.30-PM.png\" alt=\"Screen Shot 2016-06-16 at 2.20.30 PM\" width=\"520\" height=\"88\" \/><\/a>You probably see the other two storage entries:<\/p>\n<ul>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/IndexedDB_API\">IndexedDB<\/a> is a low-level\u00a0API for client-side storage of significant amounts of structured data, including files\/blobs.<\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\/sessionStorage\">Session Storage<\/a>\u00a0 &#8211; The <code>sessionStorage<\/code> property allows you to access a session <a title=\"The Storage interface of the Web Storage API provides access to the session storage or local storage for a particular domain, allowing you to for example add, modify or delete stored data items.\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Storage\"><code>Storage<\/code><\/a> object. sessionStorage is similar to <a title=\"The localStorage property allows you to access a local Storage object. localStorage is similar to sessionStorage. The only difference is that, while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the browsing session ends\u2014that is, when the browser is closed.\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\/localStorage\"><code>Window.localStorage<\/code><\/a>, the only difference is while data stored in localStorage has no expiration set, data stored in sessionStorage gets cleared when the page session ends.<\/li>\n<\/ul>\n<p>Persistence differences! Well that&#8217;s really good to know about local vs session.\u00a0 Browser support as of this date:<\/p>\n<p><a href=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-16-at-2.24.55-PM.png\" rel=\"attachment wp-att-1623\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1623\" src=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-16-at-2.24.55-PM.png\" alt=\"Screen Shot 2016-06-16 at 2.24.55 PM\" width=\"495\" height=\"171\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Localstorage is a newer way to store data locally in a browser, kind of like a cookie.\u00a0 It is supposed to have a much easier interface than cookies and can do JSON.\u00a0 Another developer I know was using this and she was using localstorage to track local data\u00a0 for some dynamic web interactions.\u00a0 Nothing will [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1615"}],"collection":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1615"}],"version-history":[{"count":9,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1615\/revisions"}],"predecessor-version":[{"id":1633,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1615\/revisions\/1633"}],"wp:attachment":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1615"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}