zoukankan      html  css  js  c++  java
  • JQuery Cookie

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    	String path = request.getContextPath();
    	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    	pageContext.setAttribute("path", path);
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>Cookie Test</title>
        <script type="text/javascript" src="${path }/js/lhgdialog/jquery-1.7.2.min.js"></script>
        <script type="text/javascript">
    		function set(){
    			$.cookie("menu", "测试!");  //设置cookie的值
    			alert("setCookie SUCCESS!");
    		}
    		function show(){
    			var menu = $.cookie("menu");	//取cookie的值
    			
    			if(menu != null){
    				$("#d").html(menu);
    			}else{
    				$("#d").html("cookie为空!");
    			}
    		}
    		function del(){
    			 $.cookie("menu", null);  //删除cookie
    			 alert("deleteCookie SUCCESS!");
    		}
    		
    		jQuery.cookie = function(name, value, options) {
    		    if (typeof value != 'undefined') { // name and value given, set cookie
    		        options = options || {};
    		        if (value === null) {
    		            value = '';
    		            options.expires = -1;
    		        }
    		        var expires = '';
    		        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
    		            var date;
    		            if (typeof options.expires == 'number') {
    		                date = new Date();
    		                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
    		            } else {
    		                date = options.expires;
    		            }
    		            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
    		        }
    		        // CAUTION: Needed to parenthesize options.path and options.domain
    		        // in the following expressions, otherwise they evaluate to undefined
    		        // in the packed version for some reason...
    		        var path = options.path ? '; path=' + (options.path) : '';
    		        var domain = options.domain ? '; domain=' + (options.domain) : '';
    		        var secure = options.secure ? '; secure' : '';
    		        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    		    } else { // only name given, get cookie
    		        var cookieValue = null;
    		        if (document.cookie && document.cookie != '') {
    		            var cookies = document.cookie.split(';');
    		            for (var i = 0; i < cookies.length; i++) {
    		                var cookie = jQuery.trim(cookies[i]);
    		                // Does this cookie string begin with the name we want?
    		                if (cookie.substring(0, name.length + 1) == (name + '=')) {
    		                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
    		                    break;
    		                }
    		            }
    		        }
    		        return cookieValue;
    		    }
    		};
        </script>
      </head>
      
      <body>
    		<a href="javascript:void();" onclick="set();">setCookie</a><br/><br/>
    		<a href="javascript:void();" onclick="show();">getCookie</a><br/>
    		show Cookie:<div id="d"></div><br/><br/><br/>
    		<a href="javascript:void();" onclick="del();">deleteCookie</a>
      </body>
    </html>


  • 相关阅读:
    目标检测——Faster R_CNN使用smooth L1作为bbox的回归损失函数原因
    [LeetCode] 2. Add Two Numbers
    XAF 非持久化的详细视图界面全部清空
    4月份开发的问题汇总
    XAF GroupOperator合并查询筛选条件
    C#判断字符判断为空或者空格
    如何去掉C#字符串前后的空格
    IIS 发布出去未能加载文件或程序集“UHFReader”或它的某一个依赖项。试图加载格式不正确
    《图解HTTP》笔记
    Win10中的控制台程序会被鼠标单击暂停运行的解决办法
  • 原文地址:https://www.cnblogs.com/jasontec/p/9601695.html
Copyright © 2011-2022 走看看