Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8816693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:40:39+00:00 2026-06-14T04:40:39+00:00

I am working from this thread: What is the shortest function for reading a

  • 0

I am working from this thread:

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

Which shows that a cookie can be read in JavaScript using the following code:

function read_cookie(key)
{
    var result;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? (result[1]) : null;
}

I have a cookie which is set called “country” and its value is two letters, for example “AZ”. I am wondering how one goes about reading the value of the cookie and putting it into a variable called cookie_country for example. This regexp is a little above me and I dont know where to start.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:40:40+00:00Added an answer on June 14, 2026 at 4:40 am

    This…

    var cookie_country = read_cookie('country');
    

    … should be enough for this case. ) The point of this function (as many others) is that it provides you just a tool for extracting a value from document.cookie string by some string key ('country', in your case): you don’t need to know how exactly this tool works unless it breaks. )

    …But anyway, perhaps it would be interesting to know this as well. ) document.cookie is actually a string containing a semicolon-separated list of cookies (its key and its value). Here’s more to read about it (MDN).

    The regex basically catches the part of this string that matches the given string either at the beginning of the line or at the end of some ;, then goes on until, again, either the string ends of another semicolon appears.

    For example, if key parameter is equal to country, the regex will look like this:

    /(?:^|; )country=([^;]*)/
    

    … and it will capture the value that is stored in document.cookie associated with country string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why this query doesn’t working: SELECT name FROM ( SELECT name FROM table1 UNION
I have this working query that outputs URL and click counts from database. <?php
I have this tabs that are working perfect in any browser apart from IE
Insert ( NSFetchedResultsChangeInsert )is working from my background thread, from that same thread, using
I have taken the working code from this thread: Creating Next and Previous buttons
Hi my face detection thread is working too slow, I call this thread from
From this page: http://www.doctrine-project.org/documentation/manual/1_2/en/working-with-models#dealing-with-relations:creating-related-records You can see that it says $obj['property']; is the recommended
I have been working on the following problem from this book . A certain
Im having trouble working grabbing a dynamically created image from this service: http://opsin.ch.cam.ac.uk/opsin/USER-STRUCTURE-TO-GENERATE.png Im
Currently I'm working on to display Epub content in UIWebview . From this link

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.