使用JS擷取SessionStorage的值,jssessionstorage

來源:互聯網
上載者:User

使用JS擷取SessionStorage的值,jssessionstorage

擷取sessionStorage的意義

首先擷取它是為了將獲得的資訊輸出或者alert();讓人容易看到,

其次,在靜態頁面中,如果使用sessionStorage就相當於在動態網頁面裡串連了資料庫一樣

例如:我上一篇所做的為button按鈕添加斷行符號事件的項目中所用到的可以使用js中的sessionStorage擷取頁面輸入的資訊,也可以獲得後台計算所得的資料,並且顯示出來。

廢話不多說,看代碼重要:

具體實現

<script type="text/javascript">    function login(){      var username=window.document.getElementById("username").value;      var password=window.document.getElementById("password").value;      if(password=="123456")      {        window.sessionStorage.setItem("username", username);        window.location.href="../index1.html" rel="external nofollow" ;      }else{        alert("密碼錯誤請輸入正確的密碼,例如:123456!");        return false;      }    }</script><input type="text" id="username" class="11" placeholder="請輸入真實姓名"/><input type="password" id="password" placeholder="請輸入密碼(預設密碼123456)"/><input type="button" value="登入考試" onclick="login()">

以上代碼是擷取username的值並傳到下一個介面

並且獲得password的值與事先設定好的對比,不同就是錯誤 就不可以登入

<script>   $(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" ;     });  }) </script>

擷取前段頁面輸入的值並且顯示至對應的位置

<div id="yhm"></div> 

並且判斷是否有sessionStorage的值,如果沒有,自動返回登入頁面,並做出相應的提示

點擊事件觸發後清空sessionStorage的值

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

當點擊id為esc的按鈕時觸發clear事件

<button id="esc" style="background-color: #FF0000">退出考試系統</button> 

則自動返回登入介面

總結

以上所述是小編給大家介紹的使用JS擷取SessionStorage的值,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.