10 examples of 'get cookie value in javascript' in JavaScript

Every line of 'get cookie value in javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
23function get_cookie (key) {
24 var i = document.cookie.indexOf (key+'=');
25 if (i < 0) {
26 return;
27 }
28
29 i += key.length + 1;
30
31 var e = document.cookie.indexOf(';', i);
32 if (e < 0) {
33 e = document.cookie.length;
34 }
35
36 return unescape (document.cookie.substring (i, e));
37}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

22function get_cookie(Name) {
23 var search = Name + "="
24 var returnvalue = "";
25 if (document.cookie.length > 0) {
26 offset = document.cookie.indexOf(search)
27 if (offset != -1) { // if cookie exists
28 offset += search.length
29 // set index of beginning of value
30 end = document.cookie.indexOf(";", offset);
31 // set index of end of cookie value
32 if (end == -1)
33 end = document.cookie.length;
34 returnvalue=unescape(document.cookie.substring(offset, end))
35 }
36 }
37 return returnvalue;
38}
26function get_cookie(Name, CookieStr="") {
27 var search = Name + "="
28 var returnvalue = "";
29 if (CookieStr.length > 0) {
30 var sd = CookieStr.indexOf(search);
31 if (sd!= -1) {
32 sd += search.length;
33 var end = CookieStr.indexOf(";", sd);
34 if (end == -1){
35 end = CookieStr.length;
36 }
37 returnvalue = window.unescape(CookieStr.substring(sd, end));
38 }
39 }
40 return returnvalue;
41}
1847function Get_Cookie(name) {
1848
1849 var start = document.cookie.indexOf(name + "=");
1850 var len = start + name.length + 1;
1851 if ((!start) &&
1852(name != document.cookie.substring(0, name.length))) {
1853 return null;
1854 }
1855 if (start == -1) return null;
1856 var end = document.cookie.indexOf(";", len);
1857 if (end == -1) end = document.cookie.length;
1858 return unescape(document.cookie.substring(len, end));
1859 }
129function readCookie (cookieName)
130{
131 var cookieMatchResult = document.cookie.match ( '(^|;) ?' + cookieName + '=([^;]*)(;|$)' );
132 if (cookieMatchResult )
133 return ( unescape (cookieMatchResult[2] ) );
134 else
135 return null;
136};
22function getCookie(cookieName) {
23 var name = cookieName + "=";
24 var ca = document.cookie.split(';');
25 for (var i = 0; i < ca.length; i++) {
26 var c = ca[i];
27 while (c.charAt(0) === ' ') c = c.substring(1);
28 if (c.indexOf(name) !== -1) return c.substring(name.length, c.length);
29 }
30 return null;
31}
53function getCookie(name) {
54 var re = new RegExp(name + "=([^;]+)");
55 var value = re.exec(document.cookie);
56 return (value != null) ? unescape(value[1]) : null;
57}
32function getCookie(name) {
33 var results = document.cookie.match ( '(^|;) ?' + name + '=([^;]*)(;|$)' );
34
35 if ( results )
36 return ( unescape ( results[2] ) );
37 else
38 return null;
39 }
124function _readCookie(name) {
125 var nameEQ = name + "=";
126 var ca = document.cookie.split(';');
127 for(var i=0;i < ca.length;i++) {
128 var c = ca[i];
129 while (c.charAt(0)==' ') c = c.substring(1,c.length);
130 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
131 }
132 return null;
133}
12get(key: string): any {
13 if (!this.cookies[key]) {
14 let cookie = window.document
15 .cookie.split('; ')
16 .filter((item: any) => item.split('=')[0] === key).pop();
17 if (!cookie) {
18 return null;
19 }
20
21 this.cookies[key] = this.parse(cookie.split('=').pop());
22 }
23
24 return this.cookies[key];
25}

Related snippets