JavaScript Examples - Extract Cookie Value
Learn how to extract specific cookie values with JavaScript using functions for efficient A/B testing and HTTP header manipulation.
Read the value of a specific cookie by name directly on Azion’s global network and use it to shape the response. This is helpful for A/B testing, personalization, or any logic that depends on data your application already stores in the visitor’s cookies.
How it works
The helper getCookie reads the Cookie header with request.headers.get("Cookie"), splits it on ; into individual pairs, and splits each pair on = to compare the cookie name against the one you want. When a match is found, it returns that cookie’s value. The fetch handler then calls getCookie and uses event.respondWith() with a new Response, returning either the cookie value or a message noting the cookie was not present.