登录成功返回登录前页面js代码

JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的。

而cookie是运行在客户端的,所以可以用JS来设置cookie.

假设有这样一种情况,在某个用例流程中,由A页面跳至B页面,若在A页面中采用JS用变量temp保存了某一变量的值,在B页面的时候,同样需要使用JS来引用temp的变量值,对于JS中的全局变量或者静态变量的生命周期是有限的,当发生页面跳转或者页面关闭的时候,这些变量的值会重新载入,即没有达到保存的效果。解决这个问题的最好的方案是采用cookie来保存该变量的值,那么如何来设置和读取cookie呢?

首先需要稍微了解一下cookie的结构,简单地说:cookie是以键值对的形式保存的,即key=value的格式。各个cookie之间一般是以“;”分隔。

JS设置cookie:

假设在A页面中要保存变量username的值("jack")到cookie中,key值为name,则相应的JS代码为:


document.cookie="name="+username;


JS读取cookie:

假设cookie中存储的内容为:name=jack;password=123

则在B页面中获取变量username的值的JS代码如下:

1
2
3
4
5
6
7
8
9
10
var  username=document.cookie.split( ";" )[0].split( "=" )[1];
//JS操作cookies方法!
//写cookies
function  setCookie(name,value)
{
var  Days = 30;
var  exp =  new  Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name +  "=" + escape (value) +  ";expires="  + exp.toGMTString();
}

读取cookies

1
2
3
4
5
6
7
8
function  getCookie(name)
{
var  arr,reg= new  RegExp( "(^| )" +name+ "=([^;]*)(;|$)" );
if (arr=document.cookie.match(reg))
return  unescape(arr[2]);
else
return  null ;
}

删除cookies

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function  delCookie(name)
{
var  exp =  new  Date();
exp.setTime(exp.getTime() - 1);
var  cval=getCookie(name);
if (cval!= null )
document.cookie= name +  "=" +cval+ ";expires=" +exp.toGMTString();
}
//使用示例
setCookie( "name" , "hayden" );
alert(getCookie( "name" ));
//如果需要设定自定义过期时间
//那么把上面的setCookie 函数换成下面两个函数就ok;
//程序代码
function  setCookie(name,value,time)
{
var  strsec = getsec(time);
var  exp =  new  Date();
exp.setTime(exp.getTime() + strsec*1);
document.cookie = name +  "=" + escape (value) +  ";expires="  + exp.toGMTString();
}
function  getsec(str)
{
alert(str);
var  str1=str.substring(1,str.length)*1;
var  str2=str.substring(0,1);
if  (str2== "s" )
{
return  str1*1000;
}
else  if  (str2== "h" )
{
return  str1*60*60*1000;
}
else  if  (str2== "d" )
{
return  str1*24*60*60*1000;
}
}
//这是有设定过期时间的使用示例:
//s20是代表20秒
//h是指小时,如12小时则是:h12
//d是天数,30天则:d30
setCookie( "name" , "hayden" , "s20" );

实际使用例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*------ setCookie(name,value) -----------*/
function  setCookie(name,value)
{
  var  Days = 30;  //此 cookie 将被保存 30 天
  var  exp =  new  Date();
  exp.setTime(exp.getTime() + Days*24*60*60*1000);
  document.cookie = name +  "=" + escape (value) +  ";expires="  + exp +  ";path=/" ;
}
/*-------- getCookie(name) ----------*/
function  getCookie(name)
{
  var  arr = document.cookie.match( new  RegExp( "(^| )" +name+ "=([^;]*)(;|$)" ));
  if (arr != null return  unescape(arr[2]);  return  null ;
}
  
$( function (){
  if ($( "#header" )[0]){
  setCookie( "currentUrl" , window.location.href);
  }
})
调用
  var  currentUrl = getCookie( 'currentUrl' );
     setCookie( "currentUrl" "" );
  $.ajax({
     type:  "post" ,
     url:  "/Ajax/User.ashx" ,
     data: {  "method" "Login" "username" : $( ".phoneNum" ).val(),  "password" : password,  "Check_Pwd" : $( '#check' ).is( ':checked' ) },
     success:  function  (text) {
         if  (text ==  "error" ) {
                 $( ".pwd" ).focus().tips({
                     bg: '#1193f6' ,
                     msg:  '用户名或密码错误~'
                 });
         }
         else  if  (text ==  "error1" ) {
                 $( ".phoneNum" ).focus().tips({
                     bg: '#1193f6' ,
                     msg:  '该用户已被禁用~'
                 });
             }
         else  if (text ==  "success" ) {
             if  (currentUrl !=  ""  && currentUrl !=  null ) {
                     window.location.href = currentUrl;
                 else  {
                     window.location =  '/index' ;
                 }  
         }
     }
  });

本文转自  小旭依然  51CTO博客,原文链接:http://blog.51cto.com/xuyran/1876577
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值