网络编程 
首页 > 网络编程 > 浏览文章

js判断登录与否并确定跳转页面的方法

(编辑:jimmy 日期: 2026/5/2 浏览:3 次 )

本文实例讲述了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程序设计有所帮助。

上一篇:分享两个手机访问pc网站自动跳转手机端网站代码
下一篇:jQuery知识点整理
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap