javascript get cookie by name: hello friends here, JavaScript can simple create, read, and delete cookies with the document.cookie property with step by step.
javascript get cookie by name
Get Cookie By Name In Javascript
document.cookie = "fkey1 = fvalue1; fkey2 = fvalue2; expires = date";
Get cookie by name
javascript get cookie by name Example
function getCookie(name) { const cookie = `; ${document.cookie}`; const parts = cookie.split(`; ${name}=`); if (parts.length === 2) return parts.pop().split(';').shift(); }
Get a cookie by name in javascript
function getCookie(cookieName) { let cookie = {}; document.cookie.split(';').forEach(function(el) { let [key,value] = el.split('='); cookie[key.trim()] = value; }) return cookie[cookieName]; }
javascript delete cookie
function deleteCookie(name) { document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; }
javascript create cookie
function setCookie(name,value,days) { var expires = ""; if (days) { var creatd_at = new Date(); creatd_at.setTime(creatd_at.getTime() + (days*24*60*60*1000)); expires = "; expires=" + creatd_at.toUTCString(); } document.cookie = name + "=" + (value || "") + expires + "; path=/"; } function getCookie(name) { var vDq_cookies = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var ck = ca[i]; while (ck.charAt(0)==' ') ck = ck.substring(1,ck.length); if (ck.indexOf(vDq_cookies) == 0) return ck.substring(vDq_cookies.length,ck.length); } return null; } setCookie("member_email","[email protected]",30); //set "member_email" cookie, expires in 30 days var memberEmail=getCookie("member_email");//"[email protected]"
document get cookie
function setCookie(cname, cvalue, exdays = 999) { const ckd = new Date(); ckd.setTime(ckd.getTime() + exdays * 24 * 60 * 60 * 1000); const expires = 'expires=' + ckd.toUTCString(); document.cookie = cname + '=' + cvalue + ';' + expires + ';path=/'; } function getCookie(cname) { const cookies = Object.fromEntries( document.cookie.split(/; /).map(ck => { const [key, v] = ck.split('=', 2); return [key, decodeURIComponent(v)]; }), ); return cookies[cname] || ''; } setCookie('language', 'vietnam', 365); var memberEmail = getCookie('language');
For storing array inside cookie : javascript get cookie by name
setter cookies : var data_row = JSON.stringify(arr); cookie.set('mycookie', data_row); getter cookies : cookie.get('mycookie'); var arr = JSON.parse(data_row);
Don't Miss : Javascript Manage Cookies Setcookie And Getcookie
set and get cookie in javascript
Function common for all type of variable :
let cookie = { set: function(name, value) { document.cookie = name+"="+value; }, get: function(name) { let vDq_cookies = name + "="; let ca = document.cookie.split(';'); for( let i = 0; i < ca.length; i++ ) { let ck = ca[i]; while (ck.charAt(0)==' ') ck = ck.substring(1,ck.length); if (ck.indexOf(vDq_cookies) == 0) return ck.substring(vDq_cookies.length,ck.length); } return null; } }
I hope you get an idea about javascript get cookie by name.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.