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程式設計有所協助。