码迷,mamicode.com
首页 > Web开发 > 详细

Js设置及获取Cookie的方法

时间:2015-09-15 11:00:58      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

Login页面设置Cookie:

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;

} 

 

 

 

其它页面获取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>

 

Js设置及获取Cookie的方法

标签:

原文地址:http://www.cnblogs.com/colinliu/p/4809230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!