This article describes how to judge whether to log in or not and determine whether to jump to the page. Share it for your reference. The details are as follows:
Use session storage to determine whether the user is logged in, thereby determining which page the page will jump to.
Determine whether there is a customerID locally:
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"); }In html, in the corresponding A tag, just write onclick="infoJumpTo" and so on.
But how is the customerID of the login page stored in the local session?
function confirm(){ var tel=$tel.val();//Get the login name and password in the page var pwd=$pwd.val(); if(tel==""|| pwd==""){//Judge that both are not empty (other judgment rules have been judged when they are entered) alert("The mobile phone number and password cannot be empty!") return false; }else{//All the above meet the requirements, then call the login esb interface $.ajax({ url:config.baseServerUrl + '/account/login',//The corresponding esb interface address type:'post', data:{mobile:tel,password:pwd},//Parameters passed to the server (interface) success:function(data){//Data returned by the server (interface) if(data.success){//If the returned information indicates that the submitted information is the correct var customerId = data.attr.customerInfo.id;//Assign the ID of the user information in the data to the variable sessionStorage.customerId = customerId;//Storage the variable in the local sessionStorage, and the value is customerID window.location.href='http://localhost/index.html';//After login correctly, the page jumps to} else{//If the information provided by the returned information is incorrect if(tel != data.tel){//Defend it is the user name or password incorrectly, prompt the corresponding information alert(data.message); $tel.val(""); $pwd.val(""); return false; } if(pwd != data.pwd){ alert(data.message); $pwd.val(""); return false; } } } }) } }On the login page, people are usually used to click enter directly to avoid manually clicking on the login button after entering the information. The js code is as follows:
//Judge whether the Enter key $(document).keyup(function(event){ if(event.keyCode ==13){ $("#login").trigger("click"); } });I hope this article will be helpful to everyone's JavaScript programming.