Sessionstorage and localstorage of H5

Time:2019-11-15

1) difference between H5’s new sessionstorage and localstorage

 

  sessionStorageSimilar to Java session, it can store information in a short time. Computer browsers often use session storage to store user login information (personal understanding)

  localStorageIt can keep the user information permanently without logging in every time. It is often used in app

 

2) common methods of sessionstorage and localstorage:

1. Setitem

Setitem is like a map in Java, but it has generics

    sessionStorage.setItem(“user”,user);

2. Getitem value

    sessionStorage.getItem(“user”);

3. Removeitem delete value

    sessionStorage.removeItem(“user”);

4. Clear

    sessionStorage.clear();

 

3) front end conversion JSON and string

String to JSON. Parse();

Convert JSON to string JSON. Stringify();

 

 

The above is the basic usage of H5’s newly added sessionstorage and localstorage, which will be better understood with examples.

The code is as follows:

 

//If false is true, i.e. the verification code is correct, send a request to the backend
                if(flag==true){
                    $.post("http://localhost:8080/test/admin/login",{username:user,password:pwd},function (data) {
                        if(data){
                            //Setitem is like the put of map in Java, but it has generics
                            //Data is of JSON type, so use json.stringify (data) to change to string
                            var s=JSON.stringify(data);
                            sessionStorage.setItem("admin",s);
                            window.location.href="index.html";
                        }else{
                            flag=false;
                            //Restore the verification code to its original state
                            re.refresh();
                            $("ා MSG"). HTML ("wrong user name or password, please try again!!! "";
                            setTimeout(function () {
                                $("#msg").html("");
                            },2000);
                        }
                    })
                }else{
                    $("ාmsg"). HTML ("please slide the verification code!! !
                    setTimeout(function () {
                        $("#msg").html("");
                    },2000);
                }

 

 

 

 

 2019-09-09 10:18:45