Cookie Retrieval by Name
In JavaScript, the retrieval of cookie values can be achieved through the document.cookie property. However, when dealing with multiple cookies, it becomes necessary to specifically target a particular cookie.
Challenge:
Modify the existing getCookie1() function to only retrieve values from a specific cookie named "obligations".
Solution:
One effective approach is to avoid iterating over an array and instead utilize a RegExp-based method:
function getCookie(name) { const value = `; ${document.cookie}`; const parts = value.split(`; ${name}=`); if (parts.length === 2) return parts.pop().split(';').shift(); }
Explanation:
The above is the detailed content of How Can I Retrieve a Specific Cookie by Name in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!