• Js设置及获取Cookie的方法


    Login页面设置Cookie:

    <script type="text/javascript">
    
    if(json.result=="true")
        {    
            if($("input[type='checkbox']").is(':checked'))
            {setCookie('sapid',json.zstaff_id,30)}
            else
            {setCookie('sapid',json.zstaff_id,0.05)/*1小时后过期*/}
            alert("登陆成功");
            /*登陆成功后定向到主页*/
            window.location.href = "index.html";
        }
        else
        {alert("账号或密码错误,登陆失败!");}
     
    //设置Cookie
    function setCookie(cname, cvalue,cdate) {
    var exdays=cdate;
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
    } 
    </script>
    

    其它页面获取Cookie判断:

    <script type="text/javascript">
    $(document).ready(function(){
    /*未加载成功时不显示*/
        var userid = getCookie("sapid");
         if (userid == "") {
            $(".alertDiv_hint").html("<p>您无权浏览该页面,请登陆后再试!</p>");
            $(".alertDiv").show();
            setTimeout("location.href='login.html'",1000);
            }
    });
    //获取cookie
    function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1);
    if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return "";
    }
    //清除cookie
    function delCookie(name){
    var date = new Date();
    date.setTime(date.getTime() - 10000);
    document.cookie = name + "=a; expires=" + date.toGMTString();
    }
    </script>
    
    博客地址:http://www.cnblogs.com/colinliu/
    博客版权:本文以学习、记录、分享为目的。欢迎大家转载,但务必注明原文地址,谢谢合作!
  • 相关阅读:
    [20180317]12c TABLE ACCESS BY INDEX ROWID BATCHED3.txt
    模态登录
    javascript unshift()和shift()
    登录拦截功能
    springmvc 拦截器的使用小结
    handsontable 和 echarts都定义了require方法,初始化时冲突了,怎么办?
    eclipse缓慢了么?
    springmvc处理日期格式
    hibernate设置了not-null 为什么无效?
    java 和 javascript CryptoJS 进行HmacSHA1加密
  • 原文地址:https://www.cnblogs.com/colinliu/p/4809230.html
Copyright © 2020-2023  润新知