使用JS获取SessionStorage的值

前端之家收集整理的这篇文章主要介绍了使用JS获取SessionStorage的值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

获取sessionStorage的意义

首先获取它是为了将获得的信息输出或者alert();让人容易看到,

其次,在静态页面中,如果使用sessionStorage就相当于在动态页面里连接了数据库一样

例如:我上一篇所做的为button按钮添加回车事件的项目中所用到的可以使用js中的sessionStorage获取页面输入的信息,也可以获得后台计算所得的数据,并且显示出来。

废话不多说,看代码重要:

具体实现

nofollow" ; }else{ alert("密码错误请输入正确的密码,例如:123456!"); return false; } } 登录考试" onclick="login()">

以上代码获取username的值并传到下一个界面

并且获得password的值与事先设置好的对比,不同就是错误 就不可以登录

$(function () { var username= window.sessionStorage.getItem("username") $("#yhm").html("登录用户:"+username) $("#name").html(username) if(window.sessionStorage.getItem("username")===null){ alert("您还没有登录,请登录后重试!") window.location.href="Pages/index.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; } $("#esc").on("click",function(){ window.sessionStorage.clear(); window.location.href="Pages/index.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; }); })

获取前段页面输入的值并且显示至对应的位置

并且判断是否有sessionStorage的值,如果没有,自动返回登录页面,并做出相应的提示

点击事件触发后清空sessionStorage的值

$("#esc").on("click",function(){ window.sessionStorage.clear(); window.location.href="Pages/index.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; });

当点击id为esc的按钮时触发clear事件

退出考试系统

自动返回登录界面

总结

以上所述是小编给大家介绍的使用JS获取SessionStorage的值。编程之家 jb51.cc 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可分享给好友!感谢支持

原文链接:https://www.f2er.com/js/34203.html

猜你在找的JavaScript相关文章