js中cookie的操作函数

<script type="text/javascript">

    function setCookie(sName,sValue,oExpires,sPath,sDomain,bSecure){
        var sCookie = sName + "=" + encodeURIComponent(sValue);  
        //失效时间
        if(oExpires){
            sCookie += "; expires= "+ oExpires.toGMTString();
        }
        //路径
        if(sPath){
            sCookie += "; path = "+ sPath;
        }
        //域
        if(sDomain){
            sCookie += "; domain = " +sDomain;
        }
        //安全标志
        if(bSecure){
            sCookie += "; secure = " +bSecure;
        }
        // 返回完整的Cookies
       document.cookie = sCookie;
    }

    function getCookie(sName){
        //正则表示的pattern
        var sRE = "(?:; )?" + sName + "=([^;]*);?";
        //创建正则表达式对象
        var oRE = new RegExp(sRE);
        // 使用test测试是否满足定义的格式
        if(oRE.test(document.cookie)){
            return decodeURIComponent(RegExp["$1"]);
        }else{
            return null;
        }
    }
    function deleteCookie(sName,sPath,sDomain){
        var sCookie = sName + "=; expires =" + (new Date(0)).toGMTString();
        if(sPath){
            sCookie += ";path= "+ sPath;
            }
        if(sDomain){
            sCookie += ";path= "+ sDomain;
        }
        document.cookie = sCookie;
    }
</script>
<body>
    This is my JSP cookie page. <br>
    <hr>
    <script type="text/javascript">
        alert("Setting cookies");
        setCookie("name","aa");
        setCookie("info","bb");
       //get value of cookie
        alert("The value of cookie 'name' is :" + getCookie("name"));
        alert("The value of cookie 'info' is :" + getCookie("info"));
      
        alert("delete info");
        deleteCookie("info");
        alert("The value of cookie 'name2' is :" + getCookie("name"));
        alert("The value of cookie 'info2' is :" + getCookie("info"));
      
      
    </script>
</body>
</html>

你可能感兴趣的:(jsp,正则表达式)