js判斷登入與否並確定跳轉頁面的方法,js判斷跳轉頁面

來源:互聯網
上載者:User

js判斷登入與否並確定跳轉頁面的方法,js判斷跳轉頁面

本文執行個體講述了js判斷登入與否並確定跳轉頁面的方法。分享給大家供大家參考。具體如下:

使用session儲存,確定使用者是否登入,從而確定頁面跳轉至哪個頁面。

判斷本地有無customerID:

function jumpTo(p, url) {    var customerId=sessionStorage.customerId;    if (customerId == undefined) {      p.attr("href", "page/Login/login.html"); <span style="white-space:pre">  </span>} else {       p.attr("href", url);     } }  function infoJumpTo() {    var $info = $("#info");    jumpTo($info, "http://localhost/page/AmountAscension/amountAscension.html"); }  function starJumpTo() {    var $star = $("#star");    jumpTo($star, "http://localhost/page/MyAccount/myAccount.html");  }

html中,相應的a標籤中,寫onclick="infoJumpTo"等就可以了。

但登入頁面的customerID是如何儲存到本地的session中的呢?

function confirm(){   var tel=$tel.val();//擷取頁面中登入名稱和密碼   var pwd=$pwd.val();   if(tel==""|| pwd==""){//判斷兩個均不為空白(其他判斷規則在其輸入時已經判斷)     alert("手機號密碼均不可為空!")     return false;   }else{//以上均符合要求,則調用登入esb介面      $.ajax({        url:config.baseServerUrl + '/account/login',//相對應的esb介面地址       type:'post',         data:{mobile:tel,password:pwd},//向伺服器(介面)傳遞的參數       success:function(data){//伺服器(介面)返回來的資料         if(data.success){//如果返回來的資訊說明提交的資訊為正確的            var customerId = data.attr.customerInfo.id;//將資料中使用者資訊的ID賦值給變數            sessionStorage.customerId = customerId;//將變數儲存到本地sessionStorage中,並且value為customerID            window.location.href='http://localhost/index.html';//正確登入後頁面跳轉至          }          else{//如果返回來的資訊說明提供的資訊為錯誤的            if(tel != data.tel){//判斷是使用者名稱還是密碼錯誤,提示相應資訊              alert(data.message);              $tel.val("");              $pwd.val("");              return false;             }             if(pwd != data.pwd){              alert(data.message);              $pwd.val("");              return false;             }          }        }     })   } }

登入頁面,人們一般習慣輸完資訊後,直接點擊enter免除手動點擊登入按鈕,則js代碼如下:

//判斷是否敲擊了Enter鍵 $(document).keyup(function(event){     if(event.keyCode ==13){       $("#login").trigger("click");     } });

希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.