首页 > 其他 > 详细

cookie的使用方法,截取字符串

时间:2016-05-12 19:58:36      阅读:96      评论:0      收藏:0      [点我收藏+]
cookie的使用方法。<pre name="code" class="html">document.cookie = "name="+textName.value;直接就把数据存储到cookie中,如果继续存储数据的话就会当成字符串往后挨着存储。也就是说cookie就是一长串的字符串,
所有的键值对多按照先后顺序存储在里面。取出来的时候就把这个字符串按照字符截取,把相应的值取出来。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<input type="text" id="textId" />
		<input type="text" id="passId" />
		<input type="button" id="btnId" />
	</body>
	
	<script type="text/javascript">
		
		var textName = document.getElementById("textId");
		var btn = document.getElementById("btnId");
		var pass = document.getElementById("passId");
		
		 // 存储数据到cookie中
		btn.onclick = function (){
			document.cookie = "name="+textName.value;
			document.cookie = "pass="+ pass.value;
			
		}
		
		var arrCookie = document.cookie.split(";"); // 截取字符串
		for(var i=0;i<arrCookie.length;i++){
				//alert("1")
				var arrCookieItem = arrCookie[i].split("=");
				
				arrCookieItem[0] = arrCookieItem[0].replace(" ","");
				//alert(arrCookieItem[0])
				//alert(arrCookieItem)
				if(arrCookieItem[0]=="name"){
					textName.value = arrCookieItem[1]; // 赋值输入框
					//alert("fujichao")
					
				}
			}
		
		
		//alert(document.cookie);
		</script>
</html>

cookie的使用方法,截取字符串

原文:http://blog.csdn.net/sdfujichao/article/details/51354339

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!