zoukankan      html  css  js  c++  java
  • JavaScript 设置、读取Cookie

    1、设置Cookie

    //设置cookie
            function setCookie(cookieName, cookieValue, cookieExpires, cookiePath) {
                cookieValue = escape(cookieValue);//编码latin-1
                if (cookieExpires == "") {
                    var nowDate = new Date();
                    nowDate.setMonth(nowDate.getMonth() + 6);
                    cookieExpires = nowDate.toGMTString();
                }
                if (cookiePath != "") {
                    cookiePath = ";Path=" + cookiePath;
                }
                document.cookie = cookieName + "=" + cookieValue + ";expires=" + cookieExpires + cookiePath;
            }

    2、读取Cookie

    //获取cookie
            function getCookieValue(cookieName) {
                var cookieValue = document.cookie;
                var cookieStartAt = cookieValue.indexOf("" + cookieName + "=");
                if (cookieStartAt == -1) {
                    cookieStartAt = cookieValue.indexOf(cookieName + "=");
                }
                if (cookieStartAt == -1) {
                    cookieValue = null;
                }
                else {
                    cookieStartAt = cookieValue.indexOf("=", cookieStartAt) + 1;
                    cookieEndAt = cookieValue.indexOf(";", cookieStartAt);
                    if (cookieEndAt == -1) {
                        cookieEndAt = cookieValue.length;
                    }
                    cookieValue = unescape(cookieValue.substring(cookieStartAt, cookieEndAt));//解码latin-1
                }
                return cookieValue;
            }

    3、调用,使用方法

            function set()
            {
                setCookie("userName", "ck", "", "");
            }
            function read()
            {
                var value = getCookieValue("userName");
                alert(value);
            }
  • 相关阅读:
    ansj分词原理
    于erlang依赖的linux调优
    Gearman
    生成具有三态背景图片的按钮
    tracert
    Wings 3D
    jira
    Erlang编程语言的一些痛点
    QTreeView 限制特定的深度、特定深度下的列 是否可以编辑
    QStandardItemModel角色控制及QTreeView添加不同的右键菜单
  • 原文地址:https://www.cnblogs.com/ck168/p/5501474.html
Copyright © 2011-2022 走看看