Js COOkie 读取

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
    <title>cookie处理函数练习(为我所写,非我所想:改善面向对象)</title>
    <script language="JavaScript" type="text/javascript">
        function addCookie(objName, objValue, objHours) {
            //添加cookievar
            str = objName + "=" + escape(objValue);
            if (objHours > 0) {
                //为0时不设定过期时间,浏览器关闭时cookie自动消失
                var date = new Date();
                var ms = objHours * 3600 * 1000; date.setTime(date.getTime() + ms);
                str += "; expires=" + date.toGMTString();
            }
            document.cookie = str; alert("添加cookie成功");
        }
        function getCookie(objName) {
            //获取指定名称的cookie的值
            var arrStr = document.cookie.split("; ");
            for (var i = 0; i < arrStr.length; i++) {
                var temp = arrStr[i].split("="); if (temp[0] == objName)
                    return unescape(temp[1]);
            }
        }
        function delCookie(name) {
            //为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间
            var date = new Date(); date.setTime(date.getTime() - 10000); document.cookie = name + "=a; expires=" + date.toGMTString();
        }
        function allCookie() {
            //读取所有保存的cookie字符串
            var str = document.cookie;
            if (str == "")
            { str = "没有保存任何cookie"; } alert(str);
        } function $(m, n) { return document.forms[m].elements[n].value; }
        function add_() {
            var cookie_name = $("myform", "cookie_name");
            var cookie_value = $("myform", "cookie_value");
            var cookie_expireHours = $("myform", "cookie_expiresHours");
            addCookie(cookie_name, cookie_value, cookie_expireHours);
        } function get_() {
            var cookie_name = $("myform", "cookie_name");
            var cookie_value = getCookie(cookie_name); alert(cookie_value);
        } function del_() {
            var cookie_name = $("myform", "cookie_name"); delCookie(cookie_name);
            alert("删除成功");
        }</script>
</head>
<body>
    <form name="myform">
        <div>
            <label for="cookie_name">名称</label><input type="text" name="cookie_name" />
        </div>
        <div>
            <label for="cookie_value">
                值</lable><input type="text" name="cookie_value" />
        </div>
        <div>
            <label for="cookie_expireHours">
                多少个小时过期</lable><input type="text" name="cookie_expiresHours" />
        </div>
        <div>
            <input type="button" value="添加该cookie" onclick="  add_()" />
            <input type="button" value="读取所有cookie" onclick="allCookie()" />
            <input type="button" value="读取该名称cookie" onclick="get_()" />
            <input type="button" value="删除该名称cookie" onclick=" del_()" />
        </div>
    </form>
    <hr />
</body>
</html>

Js COOkie 读取

时间: 2024-10-27 19:53:07

Js COOkie 读取的相关文章

JS设置读取删除cookie及表单交互

学习cookie和表单交互留下的一点笔记 什么是cookie?cookie 是存储于客户端的变量.当设备请求页面时,就会发送cookie.首先需要稍微了解一下cookie的结构,简单地说:cookie是以键值对的形式保存的,即key=value的格式.各个cookie之间一般是以";"分隔.JS设置cookie:document.cookie= key + '=' + value + ';expires=' + Date;其中Date为cookie的过期时间.实际案例: //setCo

js 获取读取cookie

// --- 设置cookie function setCookie(sName, sValue, expireHours) { var cookieString = sName + "=" + escape(sValue); //;判断是否设置过期时间 if (expireHours > 0) { var date = new Date(); date.setTime(date.getTime + expireHours * 3600 * 1000); cookieString

JS cookie的使用

js设置cookie有很多种方法. 第一种:(这个是w3c官网的代码) <script> //设置cookie function setCookie(cname, cvalue, exdays,cpath) {//注:JS中的函数调用可以和定义时参数个数不同,调用时如不需要可以不加后两个参数或最后一个参数) var d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); var expires = "expir

JS - Cookie: getCookie, setCookie

JS function for Cookie 如果cookie未设置,判断时与‘’比较: 1 function setCookie(cname, cvalue, exdays) { 2 var d = new Date(); 3 d.setTime(d.getTime() + (exdays*24*60*60*1000)); 4 var expires = "expires="+d.toUTCString(); 5 document.cookie = cname + "=&q

js cookie

1. 设置COOKIE function setCookie(cname,cvalue,exdays) { var d = new Date(); d.setTime(d.getTime()+(exdays*24*60*60*1000)); var expires = "expires="+d.toGMTString(); document.cookie = cname + "=" + cvalue + "; " + expires; } 函数解

js下读取input中的value值

很多人(包括我),总想像以前操作js一样,读取到input中的值:document.getElementById('').value; 结果事实证明这样读到得是null. eval(document.getElementById('')).value

Cookie中图片的浏览记录与cookie读取servle时路径的设置(文字描述)

public class ShowServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletR

js cookie设置最大过期时间 Infinity

Note: 对于永久cookie我们用了Fri, 31 Dec 9999 23:59:59 GMT作为过期日.如果你不想使用这个日期,可使用世界末日Tue, 19 Jan 2038 03:14:07 GMT,它是32位带符号整数能表示从1 January 1970 00:00:00 UTC开始的最大秒长(即01111111111111111111111111111111, 是 new Date(0x7fffffff * 1e3)). 对应 js 操作 <script> //cookie fun

(转)HttpURLConnection模拟登录后添加cookie读取网页

package fileTest; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; public class ConnTest { public static void main(String args[]) throws Exception{ String lianzaiUrl = "http://tieba.baidu.com/p/1243174814?pn="; String lo