HTML5 introduces two mechanisms, similar to HTTP session cookies, for storing structured data on the client side and overcoming the following shortcomings.
Cookies are included in every HTTP request, causing the transfer of the same data to slow down our web application.
Cookies are included with every HTTP request, resulting in unencrypted data being sent over the internet.
Cookies can only store a limited 4KB of data, which is not enough to store the required data.
The two storage methods are session storage and local storage, which will be used to handle different situations.
Almost all recent browsers support HTML5 storage, including Internet Explorer.
Session Storage
_Session Storage_ is designed for scenarios where users perform a single transaction, but multiple transactions can be performed in different windows at the same time.
Example
For example, if a user purchases a flight ticket in two different windows on the same website. If the website uses cookies to track the tickets a user has purchased, the currently purchased tickets will be "leaked" when going from one window to the other as the user clicks through the page, which could result in the user purchasing two tickets for the same flight without noticed.
HTML5 introduced the sessionStorage attribute. This website can be used to add data to the session storage. The user can still access any page of the same site in the open window holding the session. When the window is closed, the session will also lost.
The following code will set a session variable and then access the variable:
Local Storage
_Local Storage_ is designed to be stored across multiple windows and persists on the current session. In particular, web applications may want to store millions of bytes of user data on the client for performance reasons, such as entire documents written by a user or a user's mailbox.
Cookies don't handle this situation very well because they are transmitted with every request.
Example
HTML5 introduced the localStorage attribute, which can be used to access the local storage area of the page without time limits. Local storage is available whenever we use this page.
The following code sets a local storage variable that can be accessed every time this page is accessed, even the next time the window is opened:
Easy to learn the above concepts - please practice online.
Delete Web Storage
Storing sensitive data on your local machine can be dangerous and may leave security holes.
_Session Stored Data_ will be deleted by the browser immediately after session termination.
To clear local storage settings, you need to call localStorage.remove('key'); this 'key' is the key corresponding to the value we want to remove. If you want to clear all settings, you need to call the localStorage.clear() method.
The following code will completely clear local storage:
Web SQL 数据库
Web SQL 数据库 API 并不是 HTML5 规范的一部分,但是它是一个独立的规范,引入了一组使用 SQL 操作客户端数据库的 APIs。
核心方法
下面是规范中定义的三个核心方法。也会涵盖在本教程中:
openDatabase:这个方法使用现有的数据库或者新建的数据库创建一个数据库对象。
transaction:这个方法让我们能够控制一个事务,以及基于这种情况执行提交或者回滚。
executeSql:这个方法用于执行实际的 SQL 查询。
开启数据库
如果数据库已经存在,openDatabase 方法负责开启数据库,如果不存在,这个方法会创建它。
使用下面的代码可以创建并开启一个数据库:
The above method accepts the following five parameters:
Database name
Version number
Description text
Database size
Creation callback
The last and fifth parameter, the creation callback will be called after the database is created. However, even without this feature, the runtime will still create the database and the correct version.
Execute the query
To execute the query, you need to use the database.transaction() function. This function takes one parameter, which is a function responsible for actually executing the query, as shown below:
The above query statement will create a table called LOGS in the 'mydb' database.
Insert operation
To create an entry in the table, we add a simple SQL query to the above example as follows:
You can also pass dynamic values like this when creating an entry:
Here e_id and e_log are external variables, executeSql will map each entry in the array parameter to "?".
Read operation
To read an already existing record, we can use a callback to capture the result as follows:
Found rows: "
len "";Final Example
Finally, let’s put this example into a full HTML5 document like this and try running it in Safari:
在浏览器中这会生成如下所示结果:
Found rows: 2
foobar
logmsg