HTTP协议

HTTP:超文本传输协议,用于web服务器传输到本地浏览器的传输协议,它是一个无状态协议

cookie

是指缓存在本地客户端的数据

  1. //查询cookie
  2. console.log(document.cookie)
  3. //设置cookie
  4. document.cookie = "username = honny"
  5. var date = new Date();
  6. date.setDate(date.getDate()+3)
  7. document.cookie = "username=honny;expires="+date;//设置到期时间为三天后
  8. //修改cookie
  9. document.cookie = "username = honny"
  10. document.cookie = "username = honny2"
  11. //删除cookie
  12. var date = New Date()
  13. date.setDate(date.getDate()-2)
  14. document.cookie = "username = honny;expries="+date;//设置一个过去时间即为删除

cookie操作的封装

setCookie()

  1. var setCookie = function(name,value,day){
  2. var date = new Date()
  3. date.setDate(date.getDate()+day)
  4. document.cookie = name +'='+ value +';expries='+ day
  5. }
  6. setCookie(name,honey,3);
  7. console.log(document.cookie)

getCookie()

  1. var getCookie = function(name){
  2. var str = document.cookie;
  3. var arr = str.split(';');
  4. for(var i = 0; i < arr.length; i++){
  5. var arr1 = arr.split('=')
  6. if(arr1[0] == name){
  7. return arr1
  8. }
  9. }
  10. }
  11. consloe.log(getCookie('name2'));

removeCookie

  1. var removeCookie = function(name) {
  2. setCookie(name,value,-1)
  3. }