码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-05-12 20:01:04      阅读:199      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!