Thursday 29 December 2011

The localStorage - HTML5

HTML5 uses JavaScript to store and access the data.
The local Storage object stores the data with no time limit. The data will be available the next day, week, or year.


create and access the data in a local Storage

Syntax:
 
<script type="text/javascript">
localStorage.lastname="Localstorage";
document.write(localStorage.lastname);
</script>

Example:

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

localStorage.lastname="Localstorage";
document.write("My Name: " + localStorage.lastname);

</script>

</body>
</html>

 
count the number of times the user has visited in a page.
Syntax:

<script type="text/javascript">
if (localStorage.pagecount)
  {
  localStorage.pagecount=Number(localStorage.pagecount) +1;
  }
else
  {
  localStorage.pagecount=1;
  }
document.write("Visits "+ localStorage.pagecount + " time(s).");
</script>

 Example:

<html>
<body>

<script type="text/javascript">

if (localStorage.pagecount)
    {
    localStorage.pagecount=Number(localStorage.pagecount) +1;
    }
else
    {
    localStorage.pagecount=1;
    }
document.write("Visits: " + localStorage.pagecount + " time(s).");

</script>

<p>Refresh the page to see the counter increase.</p>

<p>Close the browser window, and try again, and the counter will continue.</p>
</body>
</html>

No comments:

Post a Comment