Home > Web Front-end > JS Tutorial > js method to determine whether to log in or not and determine the jump page_javascript skills

js method to determine whether to log in or not and determine the jump page_javascript skills

WBOY
Release: 2016-05-16 16:16:36
Original
1360 people have browsed it

The example in this article describes the method of js to determine whether to log in or not and determine the jump page. Share it with everyone for your reference. The details are as follows:

Use session storage to determine whether the user is logged in, so as to determine which page to jump to.

Determine whether there is a local 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"); 
 }
Copy after login

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();//获取页面中登录名和密码 
  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; 
            } 
         } 
       } 
    }) 
  } 
}
Copy after login

On the login page, people are generally used to clicking enter directly after entering the information to avoid manually clicking the login button. The js code is as follows:

//判断是否敲击了Enter键 
$(document).keyup(function(event){ 
    if(event.keyCode ==13){ 
      $("#login").trigger("click"); 
    } 
});
Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template