HTML5 provides two new ways to store data on the client:
Localstorage (): Data storage with no time limit
Sessionstorage (): Data storage for a session
The following example uses the Localstroage () method to count the number of times a user accesses a page
<script type= "Text/javascript" >
if (Localstorage.pagecount)
{
Localstorage.pagecount=number (Localstorage.pagecount) +1;
}
Else
{
Localstorage.pagecount=1;
}
document.write ("Visit" +localstorage.pagecount+ "times.");
</script>
You can see the number of visits visits:1 times after you run this code. The number of times you refresh the page continues to grow. When the page is closed, it will continue to grow on its original basis.
The following example uses the Sessionstorage () method to count the number of times a page is accessed in the current session
<script type= "Text/javascript" >
if (Sessionstorage.pagecount)
{
Sessionstorage.pagecount=number (Sessionstorage.pagecount) +1;
}
Else
{
Sessionstorage.pagecount=1;
}
document.write ("Visit" +sessionstorage.pagecount+ "times.");
</script>
The visits 1 times will be displayed after the code is run. After the page is refreshed, it will continue to grow, but the data will be discarded when the page is closed, and counted back from 1 when it is opened.
HTML5 Learning Notes Client Storage data method: Localstorage (), Sessionstorage ()