What is the shortest function for reading a cookie by name in JavaScript?

前端 未结 15 2128
猫巷女王i
猫巷女王i 2020-11-22 17:17

What is the shortest, accurate, and cross-browser compatible method for reading a cookie in JavaScript?

Very often, while building stand-alone scri

15条回答
  •  臣服心动
    2020-11-22 17:40

    Here is the simplest solution using javascript string functions.

    document.cookie.substring(document.cookie.indexOf("COOKIE_NAME"), 
                              document.cookie.indexOf(";", 
                              document.cookie.indexOf("COOKIE_NAME"))).
      substr(COOKIE_NAME.length);
    

提交回复
热议问题