


HTML5 local storage localstorage, local database, sessionStorage simple usage examples_html5 tutorial skills
A very cool feature of HTML5 is web storage, which is similar to the previous cookies, but the difference is that web storage has a local capacity of 5 MB to store, while cookies are only 4K, which is completely incomparable. advantages.
Webstrange is divided into: localstorage, sessionstorage and local database.
Next I will introduce them one by one:
1. localstorage
The use of localstorage is relatively simple. The methods are:
The code is as follows :
localStorage.setItem(key,value);//保存数据 localStorage.getItem(key);//读取数据 localStorage.removeItem(key);//删除单个数据 localStorage.clear();//删除所有数据 key:localStorage.key(index);//得到某个索引的值
A small demo to show the function:
The code is as follows:
(function($){ $(function(){ $.fn.getFormParam=function(){ var serializeObj={}; var array=this.serializeArray(); var str=this.serialize(); $(array).each(function(){ if(serializeObj[this.name]){ if($.isArray(serializeObj[this.name])){ serializeObj[this.name].push(this.value); }else{ serializeObj[this.name]=[serializeObj[this.name],this.value]; } }else{ serializeObj[this.name]=this.value; } }); return serializeObj; };</p> <p> var storageFile =JSON.parse(window.localStorage.getItem('demo')); $.each(storageFile, function(i, val){ $('#demoForm').find('[name="'+i+'"]').val(val); });</p> <p> $('#demoForm').find('[type="submit"]').on('click', function(){ var data = $('#demoForm').getFormParam(); window.localStorage.setItem('demo', JSON.stringify(data)); return false; }); }); })(jQuery)
html code:
<!doctype html> <html> <head> <meta charset="UTF-8"> <script src="jquery-1.10.2.min.js"></script> <script src="demo.js"></script> <title>Document</title> </head> <body> <form id="demoForm"> <p><label><span>姓名</span><input name="name"></label></p> <p><label><span>年龄</span><input name="age"></label></p> <p><label><span>学号</span><input name="number"></label></p> <p><label><span>地址</span><input name="address"></label></p> <p><label><span>爱好</span><input name="habit"></label></p> <p><label><span>其他</span><textarea name="big" id="" cols="30" rows="10"></textarea></label></p> <p><input type="submit" value="提交"></p> </form> </body> </html>
In this way, a simple demo showing localstorage is realized
2. sessionStorage
The usage of sessionStorage is the same as that of localStorage, but sessionStorage will be cleared when the browser closes the website, and localStorage will always be saved to the browser, and the two can be used together as appropriate.
3. Local database
Students who are familiar with IOS/Android development should be familiar with SQLite databases
The operation of the database in html5 is relatively simple, mainly including the openDatabase method and the transaction method
Use an object db to receive the object created by openDatabase to access the database
var db = openDatabase(databasename,version,description,size)
where
databasename: database name
version: database version is optional
desription: database description
size : Database allocation space size
The transaction method uses a callback function as a parameter, and executes a specific method of accessing the database in the function
db.transaction(function(tx)){ tx.executeSql(sqlQuery,[value1,value2..],dataHandler,errorHandler) });
The four parameters of the executeSql method are:
sqlQuery: the sql statement that needs to be executed specifically, create||select||update||delete;
[value1, value2..]: the array of all parameters used in the sql statement, in In the executeSql method, first replace the parameters to be used in the sql statement with "?", and then put these parameters into an array in sequence and put them in the second parameter;
dataHandler: execution success callback function;
errorHandler: execution failure callback function;
The above is the content of simple usage examples of localstorage, local database and sessionStorage of html5 local storage_html5 tutorial skills. For more related content, please pay attention to the PHP Chinese website (www. php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
