cookie

cookie的重要字段

[name][value][domain][path][expires][httponly][secure]

domain默认是本域;

path默认是目标界面的路径。例如guiqing.com/admin/login.html页面通过javascript来设置一个cookie,那么path值就是/admin/。那么guiqing/other/index.html的界面就无法获得

guiqing.com/admin/login.html设置的cookie了,除非用iframe

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>cookieb</title>
</head>
<body>
	<button type="button" onclick="setCookie()">设置cookie</button>
	<button type="button" onclick="setCookie1()">设置cookie</button>
	<button type="button" onclick="getCookie()">获取cookie</button>
	<script>
		window.onload = function(){
			var iframeEle = document.createElement("iframe");
			iframeEle.src = "http://127.0.0.1:8080/cookie1/a.html";

			document.getElementsByTagName('body')[0].appendChild(iframeEle);
			iframeEle.onload = function(){
				var childDom = iframeEle.contentDocument || iframeEle.contentWindow.document;
				alert(childDom.cookie);
			}
		}
		function setCookie(){
			document.cookie="guiqing=cookieB";
		}
		function setCookie1(){
			document.cookie="guiqing=cookieB1";
		}
		function getCookie(){
			var guiqing = document.cookie;
			alert(guiqing);
		}
	</script>
</body>
</html>

httpOnly是指仅在HTTP层面上传输的cookie,客户端脚本就无法读写该cookie了

secure指的是设置了secure标志的cookie仅在HTTPS层面上安全传输,如果是http的,就不会带上这个cookie。

本地cookie和内存cookie

内存cookie在浏览器关掉后就没了

本地存储主要包括: 本地cookie,localStorage,Flash Cookie

posted @ 2017-12-14 00:38  潮哥  阅读(151)  评论(0编辑  收藏  举报