Web Storage js

html 5 introduces a new way to store our data locally. previously it was called as cookies but we can e store max of 4kb quite a tooo small one.so in the latest html5, dev can sotre data locally upto 10mbs. this can be done using web Storage. it has two branches.

1. sessionStorage and
2. localStorage

sessionStorage holds the data till the browser is open. when it is closed, all data gone.
localStorage holds data for long time. so no need to worry abt the loss of data.

here is sample code…

sign_up.html contain the js code which store the data locally

function signup() {
var dt = new Date();
var utcDate = dt.toGMTString();
alert(utcDate);
if(typeof(Storage) !== “undefined”) {
localStorage.setItem(document.getElementById(‘un’).value,
[document.getElementById('ps').value,document.getElementById('date').value,document.getElementById('email').value]);

window.location.href=’index.html’;
}
else{
alert(“doest not support”);
}
}

here the key is user name and the value is an array which hold data,email and password.

login.html

function check() {

if(typeof(Storage) !== “undefined”) {
var array=[];
str=localStorage.getItem(document.getElementById(‘un’).value);
var arr = str.split(“,”) ;

if(document.getElementById(‘ps’).value == arr [0]){
alert(“Sucess… u r redirecting to the main page”);
window.location.href=”main.html”;
}
else{
alert(“Invalide username or password”);
}

}
else{
alert(“browser not support”);
}

}

here it gets the user name and check with the key value pair and check for the correct password.

happy coding:)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s