zoukankan      html  css  js  c++  java
  • cookie 二:

    本篇随笔从cookie的入门开始,介绍了cookie的设置获取和移除,还有一些小的应用案例:

    一、设置cookie
    <script>
    //设置cookie:
    function setCookie(name,value,iDay){
    if(iDay){
    var oDate=new Date();
    oDate.setDate(oDate.getDate()+3);
    document.cookie=name+'='+value+';path=/;expires='+oDate;
    }else{
    document.cookie=name+'='+value+';path=/;';
    }
    }
    setCookie('zhangsan','100',3);
    setCookie('lisi','200');

    </script>

    二、获取cookie:
    <script>
    //设置cookie:
    function setCookie(name,value,iDay){
    if(iDay){
    var oDate=new Date();
    oDate.setDate(oDate.getDate()+3);
    document.cookie=name+'='+value+';path=/;expires='+oDate;
    }else{
    document.cookie=name+'='+value+';path=/;';
    }
    }
    setCookie('zhangsan','100',3);
    setCookie('lisi','200');
    function getCookie(name){
    var arr = document.cookie.split('; ');
    for(var i =0 ;i < arr.length; i++){
    var tmp = arr[i].split('=');
    if(name == tmp[0]){
    return tmp[1];
    }
    }
    return '';
    }
    alert(getCookie('lisi'));
    </script>

    三、移除cookie:
    <script>
    //name:cookie名字,value:cookie 值; iDay: 过期时间
    //setCookie(name,value,iDay);
    function setCookie(name,value,iDay){
    if(iDay){
    var oDate = new Date();
    oDate.setDate(oDate.getDate() + iDay);
    document.cookie = name+'='+value+';path=/;expires=' + oDate;
    }else{
    document.cookie = name+'='+value+';path=/';
    }
    }

    setCookie('zhangsan','100',3);
    setCookie('lisi','200');

    //getCookie(name);
    //a=1; abc=123
    function getCookie(name){
    var arr = document.cookie.split('; ');
    for(var i =0 ;i < arr.length; i++){
    var tmp = arr[i].split('=');
    if(name == tmp[0]){
    return tmp[1];
    }
    }
    return '';
    }

    //removeCookie(name);
    function removeCookie(name){
    setCookie(name,'as',-1);
    }

    removeCookie('lisi');

    </script>

    四、小案例:
    1.选项卡中,离开页面时停留在一模块,再次打开时,还是那个模块。
    <style>
    #box {
    400px;
    height: 300px;
    border: #000 1px solid;
    margin: 100px auto;
    }

    #box a {
    display: block;
    float:left;
    100px;
    height: 39px;
    text-align:center;
    line-height: 39px;
    background:#ccc;
    color: #333;
    text-decoration:none;
    border-bottom: #333 1px solid;
    }

    #box a.active{
    background: #c00;
    color: #fff;
    98px;
    border-left: #333 1px solid;
    border-right: #333 1px solid;
    }

    #box div{
    400px;
    height: 260px;
    text-align:center;
    line-height:260px;
    font-size:50px;
    display:none;
    }
    </style>
    <script>
    function setCookie(name,value,iDay){
    if(iDay){
    var oDate = new Date();
    oDate.setDate(oDate.getDate()+iDay);
    document.cookie = name+'='+value+';path=/;expires='+oDate;
    }else{
    document.cookie=name+'='+value+';path=/';
    }
    }

    function getCookie(name){
    var arr = document.cookie.split('; ');
    for(var i = 0; i < arr.length; i++){
    var tmp = arr[i].split('=');
    if(name == tmp[0]){
    return tmp[1];
    }
    }
    return '';
    }
    window.onload = function(){
    var oBox = document.getElementById('box');
    var aBtn = oBox.getElementsByTagName('a');
    var aDiv = oBox.getElementsByTagName('div');
    //var index = 0;

    var index = getCookie('tabIndex');
    if(index){
    tab();
    }
    function tab(){
    for(var i = 0; i < aBtn.length; i++){
    aBtn[i].className = '';
    aDiv[i].style.display = 'none';
    }
    this.className = 'active';
    aDiv[index].style.display = 'block';
    }

    for(var i = 0; i < aBtn.length; i++){
    aBtn[i].index = i;
    aBtn[i].onclick = function(){
    index = this.index;
    tab();
    setCookie('tabIndex',this.index,10);
    }
    }
    }
    </script>
     
     
     
  • 相关阅读:
    录制caf 转 mp3
    关于百度地图iOS中 paopaoView 警告的处理方法
    iphone JB开发小记(四)theos、iosOpenDev的调试
    USB 漏洞影响超100万来自不同供应商的路由器
    进程 线程 纤程 中断
    synchronized、ReentrantLock、volatile
    TimeUnit用法
    2021年vivo互联网技术最受欢迎文章TOP25
    前端质量提升利器马可代码覆盖率平台
    zyh@163.net
  • 原文地址:https://www.cnblogs.com/beyrl-blog/p/6052479.html
Copyright © 2011-2022 走看看