正在为你努力加载中........

点滴记录 分享
3
2013-6
首页 >> javascript >> js 设置cookie(插件jquery.cookie.js)

js 设置cookie(插件jquery.cookie.js)

js操作cookie ,在官网上面找到了一个现成的实例,拿过来用吧

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js 操作cookie</title>
</head>

<body>
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="jquery.cookie.js" type="text/javascript"></script>
<script>
function set_cookie(){
	//secure是否通过https进行传输
	$.cookie('the_cookie', '4444', { expires: 7, path: '/', domain: '', secure: false });
}
function get_cookie(){
 alert($.cookie('the_cookie'));

}
</script>



<input type="button" value="设置cookie" onclick="set_cookie()"><br />
<input type="button" value="得到cookie" onclick="get_cookie()">

</body>
</html>
碰到了一个问题:我在php里面设置了cookie 并且把cookie里面的值打印了出来,,但是就是得不到值,,后来查看原因是 因为他们不
在同一个路径下面,,晕死,(我是在同一个域名下面设置的)
另外查看了discuz的设置cookie方法顺便写上
function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {
	var expires = new Date();
	if(cookieValue == '' || seconds < 0) {
		cookieValue = '';
		seconds = -2592000;
	}
	expires.setTime(expires.getTime() + seconds * 1000);
	domain = !domain ? "www.jkepd.com" : domain;
	path = !path ? cookiepath : path;
	document.cookie = escape("57sy_" + cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '/')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
}

function getcookie(name, nounescape) {
	name = "57sy_" + name;
	var cookie_start = document.cookie.indexOf(name);
	var cookie_end = document.cookie.indexOf(";", cookie_start);
	if(cookie_start == -1) {
		return '';
	} else {
		var v = document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length));
		return !nounescape ? unescape(v) : v;
	}
}


作者:一根烟的寂寞 分类:javascript
浏览:12697 评论:0