I need to get all the cookies from the browser

前端 未结 9 1326
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 09:52

I need to get all the cookies stored in my browser using JavaScript. How can it be done?

9条回答
  •  青春惊慌失措
    2020-12-04 10:49

    You can only access cookies for a specific site. Using document.cookie you will get a list of escaped key=value pairs seperated by a semicolon.

    secret=do%20not%20tell%you;last_visit=1225445171794
    

    To simplify the access, you have to parse the string and unescape all entries:

    var getCookies = function(){
      var pairs = document.cookie.split(";");
      var cookies = {};
      for (var i=0; i

    So you might later write:

    var myCookies = getCookies();
    alert(myCookies.secret); // "do not tell you"
    

提交回复
热议问题