御品VPS

御品VPS---独特的心情尽在御品VPS

HTML5 Web 存储

<div> <div> <h1>HTML5 <span>Web 存储</span> </h1>


<p>HTML5 web 存储,一个比cookie更好的本地存储方式。</p>
<h2>什么是 HTML5 Web 存储?</h2> <p>使用HTML5可以在本地存储用户的浏览数据。</p> <p>早些时候,本地存储使用的是 cookie。但是Web 存储需要更加的安全与快速. 这些数据不会被保存在服务器上,但是这些数据只用于用户请求网站数据上.它也可以存储大量的数据,而不影响网站的性能.</p> <p>数据以 键/值 对存在, web网页的数据只允许该网页访问使用。</p>
<h2>浏览器支持</h2> <p> HTML5 Web 存储HTML5 Web 存储HTML5 Web 存储HTML5 Web 存储HTML5 Web 存储</p> <p> Internet Explorer 8+, Firefox, Opera, Chrome, 和 Safari支持Web 存储。</p> <p>注意: Internet Explorer 7 及更早IE版本不支持web 存储.</p>
<h2>localStorage 和 sessionStorage </h2> <p>客户端存储数据的两个对象为:</p> <ul> <li>localStorage - 用于长久保存整个网站的数据,保存的数据没有过期时间,直到手动去除。</li> <li>sessionStorage - 用于临时保存同一窗口(或标签页)的数据,在关闭窗口或标签页之后将会删除这些数据。</li> </ul> <p>在使用 web 存储前,应检查浏览器是否支持 localStorage 和sessionStorage:</p> <div> <div> <div><span>if</span><span>(</span><span>typeof</span><span>(</span><span>Storage</span><span>)</span><span>!==</span><span>"</span><span>undefined</span><span>"</span><span>)</span><span> </span><span>{</span><span> </span><span>//</span><span> 是的! 支持 localStorage sessionStorage 对象!</span><span></span><span> </span><span>//</span><span> 一些代码.....</span><span></span><span> </span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span> </span><span>//</span><span> 抱歉! 不支持 web 存储。</span><span></span><span> </span><span>}</span></div> </div> </div>

<h2>localStorage 对象</h2> <p>localStorage 对象存储的数据没有时间限制。第二天、第二周或下一年之后,数据依然可用。</p> <div> <h2>实例</h2> <div> <div><span>localStorage</span><span>.</span><span>sitename</span><span>=</span><span>"</span><span>菜鸟教程</span><span>"</span><span>; </span><span>document</span><span>.</span><span>getElementById</span><span>(</span><span>"</span><span>result</span><span>"</span><span>)</span><span>.</span><span>innerHTML</span><span>=</span><span>"</span><span>网站名:</span><span>"</span><span> + </span><span>localStorage</span><span>.</span><span>sitename</span><span>;</span></div> </div>
</div> <p>实例解析:</p> <ul> <li>使用 key="sitename" 和 value="菜鸟教程" 创建一个 localStorage 键/值对。</li> <li>检索键值为"sitename" 的值然后将数据插入 id="result"的元素中。</li> </ul> <p>以上实例也可以这么写:</p> <div> <div> <div><span>//</span><span> 存储</span><span></span><span> </span><span>localStorage</span><span>.</span><span>sitename</span><span> = </span><span>"</span><span>菜鸟教程</span><span>"</span><span>; </span><span>//</span><span> 查找</span><span></span><span> </span><span>document</span><span>.</span><span>getElementById</span><span>(</span><span>"</span><span>result</span><span>"</span><span>)</span><span>.</span><span>innerHTML</span><span> = </span><span>localStorage</span><span>.</span><span>sitename</span><span>;</span></div> </div> </div><p>移除 localStorage 中的 "lastname" :</p> <div> <div> <div><span>localStorage</span><span>.</span><span>removeItem</span><span>(</span><span>"</span><span>lastname</span><span>"</span><span>)</span><span>;</span></div> </div> </div> <p> 不管是 localStorage,还是 sessionStorage,可使用的API都相同,常用的有如下几个(以localStorage为例):</p> <ul><li> 保存数据:localStorage.setItem(key,value);</li><li> 读取数据:localStorage.getItem(key);</li><li> 删除单个数据:localStorage.removeItem(key);</li><li> 删除所有数据:localStorage.clear();</li><li> 得到某个索引的key:localStorage.key(index);</li></ul> <p>提示: 键/值对通常以字符串存储,你可以按自己的需要转换该格式。</p> <p>下面的实例展示了用户点击按钮的次数。</p><p> 代码中的字符串值转换为数字类型:</p> <div> <h2>实例</h2> <div> <div><span>if</span><span> </span><span>(</span><span>localStorage</span><span>.</span><span>clickcount</span><span>)</span><span> </span><span>{</span><span> </span><span>localStorage</span><span>.</span><span>clickcount</span><span>=</span><span>Number</span><span>(</span><span>localStorage</span><span>.</span><span>clickcount</span><span>)</span><span>+</span><span>1</span><span>; </span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span> </span><span>localStorage</span><span>.</span><span>clickcount</span><span>=</span><span>1</span><span>; </span><span>}</span><span> </span><span>document</span><span>.</span><span>getElementById</span><span>(</span><span>"</span><span>result</span><span>"</span><span>)</span><span>.</span><span>innerHTML</span><span>=</span><span>"</span><span> 你已经点击了按钮 </span><span>"</span><span> + </span><span>localStorage</span><span>.</span><span>clickcount</span><span> + </span><span>"</span><span> 次 </span><span>"</span><span>;</span></div> </div>
</div>

<h2> sessionStorage 对象</h2> <p>sessionStorage 方法针对一个 session 进行数据存储。当用户关闭浏览器窗口后,数据会被删除。</p> <p>如何创建并访问一个 sessionStorage:</p> <div> <h2>实例</h2> <div> <div><span>if</span><span> </span><span>(</span><span>sessionStorage</span><span>.</span><span>clickcount</span><span>)</span><span> </span><span>{</span><span> </span><span>sessionStorage</span><span>.</span><span>clickcount</span><span>=</span><span>Number</span><span>(</span><span>sessionStorage</span><span>.</span><span>clickcount</span><span>)</span><span>+</span><span>1</span><span>; </span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span> </span><span>sessionStorage</span><span>.</span><span>clickcount</span><span>=</span><span>1</span><span>; </span><span>}</span><span> </span><span>document</span><span>.</span><span>getElementById</span><span>(</span><span>"</span><span>result</span><span>"</span><span>)</span><span>.</span><span>innerHTML</span><span>=</span><span>"</span><span>在这个会话中你已经点击了该按钮 </span><span>"</span><span> + </span><span>sessionStorage</span><span>.</span><span>clickcount</span><span> + </span><span>"</span><span> 次 </span><span>"</span><span>;</span></div> </div>
</div>
<h2>Web Storage 开发一个简单的网站列表程序</h2> <p>网站列表程序实现以下功能:</p> <ul><li> 可以输入网站名,网址,以网站名作为key存入localStorage;</li><li> 根据网站名,查找网址;</li><li> 列出当前已保存的所有网站;</li> </ul> <p>以下代码用于保存于查找数据:</p> <div> <h2>save() 与 find() 方法</h2> <div> <div><span>//</span><span>保存数据 </span><span></span><span> </span><span>function</span><span> </span><span>save</span><span>(</span><span>)</span><span>{</span><span> </span><span>var</span><span> </span><span>siteurl</span><span> = </span><span>document</span><span>.</span><span>getElementById</span><span>(</span><span>"</span><span>siteurl</span><span>"</span><span>)</span><span>.</span><span>value</span><span>; </span><span>var</span><span> </span><span>sitename</span><span> = </span><span>document</span><span>.</span><span>getElementById</span><span>(</span><span>"</span><span>sitename</span><span>"</span><span>)</span><span>.</span><span>value</span><span>; </span><span>localStorage</span><span>.</span><span>setItem</span><span>(</span><span>sitename</span><span>, </span><span>siteurl</span><span>)</span><span>; </span><span>alert</span><span>(</span><span>"</span><span>添加成功</span><span>"</span><span>)</span><span>; </span><span>}</span><span> </span><span>//</span><span>查找数据 </span><span></span><span> </span><span>function</span><span> </span><span>find</span><span>(</span><span>)</span><span>{</span><span> </span><span>var</span><span> </span><span>search_site</span><span> = </span><span>document</span><span>.</span><span>getElementById</span><span>(</span><span>"</span><span>search_site</span><span>"</span><span>)</span><span>.</span><span>value</span><span>; </span><span>var</span><span> </span><span>sitename</span><span> = </span><span>localStorage</span><span>.</span><span>getItem</span><span>(</span><span>search_site</span><span>)</span><span>; </span><span>var</span><span> </span><span>find_result</span><span> = </span><span>document</span><span>.</span><span>getElementById</span><span>(</span><span>"</span><span>find_result</span><span>"</span><span>)</span><span>; </span><span>find_result</span><span>.</span><span>innerHTML</span><span> = </span><span>search_site</span><span> + </span><span>"</span><span>的网址是:</span><span>"</span><span> + </span><span>sitename</span><span>; </span><span>}</span></div> </div> </div> <p>完整实例演示如下:</p> <div> <h2>实例</h2> <div> <div><span><</span><span>div</span><span> </span><span>style</span><span>=</span><span>"</span><span>border: 2px dashed #ccc;width:320px;text-align:center;</span><span>"</span><span>></span><span> </span><span><</span><span>label</span><span> </span><span>for</span><span>=</span><span>"</span><span>sitename</span><span>"</span><span>></span><span>网站名(key):</span><span></</span><span>label</span><span>></span><span> </span><span><</span><span>input</span><span> </span><span>type</span><span>=</span><span>"</span><span>text</span><span>"</span><span> </span><span>id</span><span>=</span><span>"</span><span>sitename</span><span>"</span><span> </span><span>name</span><span>=</span><span>"</span><span>sitename</span><span>"</span><span> </span><span>class</span><span>=</span><span>"</span><span>text</span><span>"</span><span>/></span><span> </span><span><</span><span>br</span><span>/></span><span> </span><span><</span><span>label</span><span> </span><span>for</span><span>=</span><span>"</span><span>siteurl</span><span>"</span><span>></span><span>网 址(value):</span><span></</span><span>label</span><span>></span><span> </span><span><</span><span>input</span><span> </span><span>type</span><span>=</span><span>"</span><span>text</span><span>"</span><span> </span><span>id</span><span>=</span><span>"</span><span>siteurl</span><span>"</span><span> </span><span>name</span><span>=</span><span>"</span><span>siteurl</span><span>"</span><span>/></span><span> </span><span><</span><span>br</span><span>/></span><span> </span><span><</span><span>input</span><span> </span><span>type</span><span>=</span><span>"</span><span>button</span><span>"</span><span> </span><span>onclick</span><span>=</span><span>"</span><span>save()</span><span>"</span><span> </span><span>value</span><span>=</span><span>"</span><span>新增记录</span><span>"</span><span>/></span><span> </span><span><</span><span>hr</span><span>/></span><span> </span><span><</span><span>label</span><span> </span><span>for</span><span>=</span><span>"</span><span>search_site</span><span>"</span><span>></span><span>输入网站名:</span><span></</span><span>label</span><span>></span><span> </span><span><</span><span>input</span><span> </span><span>type</span><span>=</span><span>"</span><span>text</span><span>"</span><span> </span><span>id</span><span>=</span><span>"</span><span>search_site</span><span>"</span><span> </span><span>name</span><span>=</span><span>"</span><span>search_site</span><span>"</span><span>/></span><span> </span><span><</span><span>input</span><span> </span><span>type</span><span>=</span><span>"</span><span>button</span><span>"</span><span> </span><span>onclick</span><span>=</span><span>"</span><span>find()</span><span>"</span><span> </span><span>value</span><span>=</span><span>"</span><span>查找网站</span><span>"</span><span>/></span><span> </span><span><</span><span>p</span><span> </span><span>id</span><span>=</span><span>"</span><span>find_result</span><span>"</span><span>></span><span><</span><span>br</span><span>/></span><span></</span><span>p</span><span>></span><span> </span><span></</span><span>div</span><span>></span></div> </div>
</div> <p>实现效果截图:</p> <p>HTML5 Web 存储</p> <p>以上实例只是演示了简单的 localStorage 存储与查找,更多情况下我们存储的数据会更复杂。</p> <p>接下来我们将使用 来存储对象数据, 可以将对象转换为字符串。</p> <div> <div> <div><span>var</span><span> </span><span>site</span><span> = </span><span>new</span><span> </span><span>Object</span><span>; ... </span><span>var</span><span> </span><span>str</span><span> = </span><span>JSON</span><span>.</span><span>stringify</span><span>(</span><span>site</span><span>)</span><span>; </span><span>//</span><span> 将对象转换为字符串</span><span></span></div> </div></div> <p>之后我们使用 方法将字符串转换为 JSON 对象:</p> <div> <div> <div><span>var</span><span> </span><span>site</span><span> = </span><span>JSON</span><span>.</span><span>parse</span><span>(</span><span>str</span><span>)</span><span>;</span></div> </div> </div> <p>JavaScript 实现代码:</p> <div> <h2>save() 与 find() 方法</h2> <div> <div><span>//</span><span>保存数据 </span><span></span><span> </span><span>function</span><span> </span><span>save</span><span>(</span><span>)</span><span>{</span><span> </span><span>var</span><span> </span><span>site</span><span> = </span><span>new</span><span> </span><span>Object</span><span>; </span><span>site</span><span>.</span><span>keyname</span><span> = </span><span>document</span><span>.</span><span>getElementById</span><span>(</span><span>"</span><span>keyname</span><span>"</span><span>)</span><span>.</span><span>value</span><span>; </span><span>site</span><span>.</span><span>sitename</span><span> = </span><span>document</span><span>.</span><span>getElementById</span><span>(</span><span>"</span><span>sitename</span><span>"</span><span>)</span><span>.</span><span>value</span><span>; </span><span>site</span><span>.</span><span>siteurl</span><span> = </span><span>document</span><span>.</span><span>getElementById</span><span>(</span><span>"</span><span>siteurl</span><span>"</span><span>)</span><span>.</span><span>value</span><span>; </span><span>var</span><span> </span><span>str</span><span> = </span><span>JSON</span><span>.</span><span>stringify</span><span>(</span><span>site</span><span>)</span><span>; </span><span>//</span><span> 将对象转换为字符串</span><span></span><span> </span><span>localStorage</span><span>.</span><span>setItem</span><span>(</span><span>site</span><span>.</span><span>keyname</span><span>,</span><span>str</span><span>)</span><span>; </span><span>alert</span><span>(</span><span>"</span><span>保存成功</span><span>"</span><span>)</span><span>; </span><span>}</span><span> </span><span>//</span><span>查找数据 </span><span></span><span> </span><span>function</span><span> </span><span>find</span><span>(</span><span>)</span><span>{</span><span> </span><span>var</span><span> </span><span>search_site</span><span> = </span><span>document</span><span>.</span><span>getElementById</span><span>(</span><span>"</span><span>search_site</span><span>"</span><span>)</span><span>.</span><span>value</span><span>; </span><span>var</span><span> </span><span>str</span><span> = </span><span>localStorage</span><span>.</span><span>getItem</span><span>(</span><span>search_site</span><span>)</span><span>; </span><span>var</span><span> </span><span>find_result</span><span> = </span><span>document</span><span>.</span><span>getElementById</span><span>(</span><span>"</span><span>find_result</span><span>"</span><span>)</span><span>; </span><span>var</span><span> </span><span>site</span><span> = </span><span>JSON</span><span>.</span><span>parse</span><span>(</span><span>str</span><span>)</span><span>; </span><span>find_result</span><span>.</span><span>innerHTML</span><span> = </span><span>search_site</span><span> + </span><span>"</span><span>的网站名是:</span><span>"</span><span> + </span><span>site</span><span>.</span><span>sitename</span><span> + </span><span>"</span><span>,网址是:</span><span>"</span><span> + </span><span>site</span><span>.</span><span>siteurl</span><span>; </span><span>}</span></div> </div> </div> <p>完整实例如下:</p> <div> <h2>实例</h2> <div> <div><span><</span><span>div</span><span> </span><span>style</span><span>=</span><span>"</span><span>border: 2px dashed #ccc;width:320px;text-align:center;</span><span>"</span><span>></span><span> </span><span><</span><span>label</span><span> </span><span>for</span><span>=</span><span>"</span><span>keyname</span><span>"</span><span>></span><span>别名(key):</span><span></</span><span>label</span><span>></span><span> </span><span><</span><span>input</span><span> </span><span>type</span><span>=</span><span>"</span><span>text</span><span>"</span><span> </span><span>id</span><span>=</span><span>"</span><span>keyname</span><span>"</span><span> </span><span>name</span><span>=</span><span>"</span><span>keyname</span><span>"</span><span> </span><span>class</span><span>=</span><span>"</span><span>text</span><span>"</span><span>/></span><span> </span><span><</span><span>br</span><span>/></span><span> </span><span><</span><span>label</span><span> </span><span>for</span><span>=</span><span>"</span><span>sitename</span><span>"</span><span>></span><span>网站名:</span><span></</span><span>label</span><span>></span><span> </span><span><</span><span>input</span><span> </span><span>type</span><span>=</span><span>"</span><span>text</span><span>"</span><span> </span><span>id</span><span>=</span><span>"</span><span>sitename</span><span>"</span><span> </span><span>name</span><span>=</span><span>"</span><span>sitename</span><span>"</span><span> </span><span>class</span><span>=</span><span>"</span><span>text</span><span>"</span><span>/></span><span> </span><span><</span><span>br</span><span>/></span><span> </span><span><</span><span>label</span><span> </span><span>for</span><span>=</span><span>"</span><span>siteurl</span><span>"</span><span>></span><span>网 址:</span><span></</span><span>label</span><span>></span><span> </span><span><</span><span>input</span><span> </span><span>type</span><span>=</span><span>"</span><span>text</span><span>"</span><span> </span><span>id</span><span>=</span><span>"</span><span>siteurl</span><span>"</span><span> </span><span>name</span><span>=</span><span>"</span><span>siteurl</span><span>"</span><span>/></span><span> </span><span><</span><span>br</span><span>/></span><span> </span><span><</span><span>input</span><span> </span><span>type</span><span>=</span><span>"</span><span>button</span><span>"</span><span> </span><span>onclick</span><span>=</span><span>"</span><span>save()</span><span>"</span><span> </span><span>value</span><span>=</span><span>"</span><span>新增记录</span><span>"</span><span>/></span><span> </span><span><</span><span>hr</span><span>/></span><span> </span><span><</span><span>label</span><span> </span><span>for</span><span>=</span><span>"</span><span>search_site</span><span>"</span><span>></span><span>输入别名(key):</span><span></</span><span>label</span><span>></span><span> </span><span><</span><span>input</span><span> </span><span>type</span><span>=</span><span>"</span><span>text</span><span>"</span><span> </span><span>id</span><span>=</span><span>"</span><span>search_site</span><span>"</span><span> </span><span>name</span><span>=</span><span>"</span><span>search_site</span><span>"</span><span>/></span><span> </span><span><</span><span>input</span><span> </span><span>type</span><span>=</span><span>"</span><span>button</span><span>"</span><span> </span><span>onclick</span><span>=</span><span>"</span><span>find()</span><span>"</span><span> </span><span>value</span><span>=</span><span>"</span><span>查找网站</span><span>"</span><span>/></span><span> </span><span><</span><span>p</span><span> </span><span>id</span><span>=</span><span>"</span><span>find_result</span><span>"</span><span>></span><span><</span><span>br</span><span>/></span><span></</span><span>p</span><span>></span><span> </span><span></</span><span>div</span><span>></span></div> </div>
</div> <p>实例中的 loadAll 输出了所有存储的数据,你需要确保 localStorage 存储的数据都为 JSON 格式,否则 JSON.parse(str) 将会报错。</p> <p>输出结果演示:</p> <p>HTML5 Web 存储</p> </div> </div>

未经允许不得转载:御品VPS » HTML5 Web 存储

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址