复制代码代码如下: Web Storage 技术在web上存储数据即针对客户端本地;具体来说分为两种: sessionStrage: session即会话的意思,在这里的session是指用户浏览某个网站时,从进入网站到关闭网站这个时间段,session对象的有效期就只有这么长。 /p p localStorage: 将数据保存在客户端硬件设备上,不管它是什么,意思就是下次打开计算机时候数据还在。 /p p 两者区别就是一个作为临时保存,一个拥有长期保存。
使用示例
复制代码代码如下: 简单应用 !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" html xmlns="http://www.w3.org/1999/xhtml" head title /title /head body h1 Web Storage 实验 /h1 div id="msg" /div input type="text" id="text" / select id="type" option value="session" sessionStorage /option option value="local" localStorage /option /select button 保存数据 /button button 读取数据 /button script type="text/javascript" var msg = document.getElementById('msg'), text = document.getElementById('text'), type = document.getElementById('type');
function save() { var str = text.value; var t = type.value; if (t == 'session') { sessionStorage.setItem('msg', str); } else { localStorage.setItem('msg', str); } }
function load() { var t = type.value; if (t == 'session') { msg.innerHTML = sessionStorage.getItem('msg'); } else { msg.innerHTML = localStorage.getItem('msg'); } }
复制代码代码如下: 简单留言板 !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" html xmlns="http://www.w3.org/1999/xhtml" head title /title /head body h1 Web Storage 实验 /h1 div id="msg" style="margin: 10px 0; border: 1px solid black; padding: 10px; width: 300px; min-height: 100px;" /div input type="text" id="text" / button 留言 /button button 清空 /button script type="text/javascript" var msg = document.getElementById('msg'), text = document.getElementById('text');
function save() { var str = text.value; var k = new Date().getTime(); localStorage.setItem(k, str); init(); }
function init() { msg.innerHTML = ''; var dom = ''; for (var i = 0, len = localStorage.length; i len; i++) { dom += ' div ' + localStorage.key(i) + ':' + localStorage.getItem(localStorage.key(i)) + ' /div ' } msg.innerHTML = dom; }
function _clear() { msg.innerHTML = ''; localStorage.clear(); }
//初始化界面 function init(name) { db.transaction(function (tx) { tx.executeSql('create table if not exists phoneBook(name text, phone text)', []); var sql = 'select * from phoneBook where 1=1'; var param = []; if (name) { sql += ' and name=? '; param.push(name); } tx.executeSql(sql, param, function (tx, rs) { phoneBook.html(''); for (var i = 0, len = rs.rows.length; i len; i++) { var data = rs.rows.item(i); showData(data); } }); }); }
function showData(data) { var str = ' div 姓名:' + data.name + ';电话:' + data.phone + ' span 删除 /span /div phoneBook.append($(str)); }
//删除数据 function deleteByName(name) { db.transaction(function (tx) { tx.executeSql('delete from phoneBook where name=?', [name], function (tx, rs) { init(); }) }); } window.del = deleteByName; //增加 function save(name, phone) { db.transaction(function (tx) { tx.executeSql('insert into phoneBook values(?, ?)', [name, phone], function (tx, rs) { var d = {}; d.name = name; d.phone = phone; showData(d); }) }); }