• cookies增删改擦操作


    //判断是否存在名为aaa的cookie
    
    function hasSetCookie(name){
        var strCookie = document.cookie;
        var arrCookie = strCookie.split("; ");
        for (var i = 0; i < arrCookie.length; i++) {
            var arr = arrCookie[i].split("=");
            if (arr[0] == name) {
                return true;
            }
        };
        return false;
    }
    
    
    //添加cookie  判断是否设置过期时间,expiresHours=0代表关闭浏览器时失效
    function addCookie(name, value, expiresHours) {
        var cookieString = name + "=" + escape(value); //escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串。
        
        if (expiresHours > 0) {
            var date = new Date();
            date.setTime(date.getTime() + expiresHours *3600* 1000);
            cookieString = cookieString + ";expires=" + date.toUTCString();
        }
        document.cookie = cookieString;
    }
    
    
    //修改cookie  判断是否设置过期时间,expiresHours=0代表关闭浏览器时失效
    function editCookie(name, value, expiresHours) {
        var cookieString = name + "=" + escape(value); //escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串。
        
        if (expiresHours > 0) {
            var date = new Date();
            date.setTime(date.getTime() + expiresHours *3600* 1000);
            cookieString = cookieString + ";expires=" + date.toUTCString();
        }
        document.cookie = cookieString;
    }
    
    
    //根据名字获取cookie的值
    function getCookie(name) {
        var strCookie = document.cookie;
        var arrCookie = strCookie.split("; ");
        for (var i = 0; i < arrCookie.length; i++) {
            var arr = arrCookie[i].split("=");
            if (arr[0] == name) {
                return unescape(arr[1]);
                break;
            } else {
                continue;
            };
        };
    }
    
    
    //删除cookie
    function deleteCookie(name){ 
        var date=new Date(); 
        date.setTime(date.getTime()-10000); 
        document.cookie=name+"=v; expires="+date.toUTCString(); 
    } 
    

      

  • 相关阅读:
    .gitignore 文件没起作用
    HTML 中img标签不显示
    关于拖拽
    关于javascript三目
    封装ajax
    javascript-时间戳
    关于Vue实例的生命周期created和mounted的区别
    ES6核心内容讲解
    jsonp跨域请求
    javascript-AJAX
  • 原文地址:https://www.cnblogs.com/zst062102/p/8072801.html
Copyright © 2020-2023  润新知