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>


posted @ 2013-03-14 13:00  問天  阅读(117)  评论(0编辑  收藏  举报