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

前端 未结 15 1895
猫巷女王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:38

    How about this one?

    function getCookie(k){var v=document.cookie.match('(^|;) ?'+k+'=([^;]*)(;|$)');return v?v[2]:null}
    

    Counted 89 bytes without the function name.

提交回复
热议问题