zoukankan      html  css  js  c++  java
  • JS设置浏览器缓存,以及常用函数整理

    //设置缓存
    function set_cache(key,value){
    if(key=='') return false;
    localStorage.setItem(key, value);
    }

    //读取缓存
    function get_cache(key){
    return localStorage.getItem(key);
    }

    //删除缓存
    function remove_cache(key){
    localStorage.removeItem(key);
    }

    //写入cookie
    function setCookie(name,value){
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days2460601000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
    }

    //获取cookie
    function getCookie(name){
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
    return unescape(arr[2]);
    else
    return null;
    }

    //删除cookie
    function delCookie(name){
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null)
    document.cookie= name + "="+cval+";expires="+exp.toGMTString();
    }

    //获取get参数
    function get(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
    }

    //公钥加密
    function encrypt(data){
    //需要引入插件jsencrypt.js 下载地址http://download.csdn.net/detail/codercwm/9770398
    var obj = new JSEncrypt();
    obj.setPublicKey(RSA_PUBLIC_KEY);
    var data = obj.encrypt(data);
    return data;
    }

    //私钥解密
    function decrypt(data){
    //需要引入插件jsencrypt.js 下载地址http://download.csdn.net/detail/codercwm/9770398
    var obj = new JSEncrypt();
    obj.setPrivateKey(RSA_PRIVATE_KEY);
    var data = obj.decrypt(data);
    return data;
    }

    //格式化时间戳
    //getLocalTime(1470800400);
    //输出:2016-08-10 11:40:00
    function getLocalTime(time){
    var datetime = new Date();
    datetime.setTime(parseInt(time+'000'));
    var year = datetime.getFullYear();

    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
    

    }

    //获取当前文件的路径
    var path = document.scripts;
    path = path[path.length - 1].src.substring(0, path[path.length - 1].src.lastIndexOf("/"));

    原文:https://blog.csdn.net/codercwm/article/details/60333223

  • 相关阅读:
    Python3.4 + Django1.7.7 搭建简单的表单并提交
    python3.4 + Django1.7.7 表单的一些问题
    TypeScript(10): String(同JS)
    TypeScript(09): Number(同JS)
    TypeScript(08): 循环
    TypeScript(07): 条件语句(同JS)
    TypeScript(06): 运算符(同JS)
    TypeScript(05): 变量声明
    TypeScript(04): 基础类型
    TypeScript(03):基础语法
  • 原文地址:https://www.cnblogs.com/panax/p/10102330.html
Copyright © 2011-2022 走看看