0

I'm helping a nonprofit design a trilingual website: English, French, and Haitian Creole. I've been able to set, change, and delete cookies for the visitors' language choice -- that part isn't the problem.

The problem is that the only way I've found to READ the cookie value uses regex. I suppose I don't have a problem with regex, per se, but I really just don't understand it...at all! I copied and pasted some code that does work, but if something ever happens that breaks it or it otherwise malfunctions, I know I won't be able to troubleshoot it or fix it. Can anyone suggest a way to use jQuery or just plain Javascript to read one value inside a cookie?

The cookie sets the following: document.cookie = "language=EN" (or FR, or HT, depending on the visitor's choice) It sets both max-age AND expires for cross-browser compatibility, and its path is set to / so it covers the whole site. This part isn't the problem.

To read it, I use the following code, which DOES work:

var lang = document.cookie.replace(/.*\blanguage=(\w+)\b.*/, "$1");
if (lang == "EN") { window.location = "http://example.com/en"; } else
if (lang == "FR") { window.location = "http://example.com/fr"; } else
if (lang == "HT") { window.location = "http://example.com/ht"; };

However, this is the part that I really don't understand. All that regex coding, and the fact that I'm using .replace(), makes no sense to me. I've played around with using .indexOf() and .toString() to read the cookie with search or other functions, but nothing seems to work.

I'm using HTML5 with jQuery 3.1.0 (not php), but like I said, I'm fine with just plain Javascript to send returning visitors to their language's home page.

I'm just trying to preserve my ability to troubleshoot in the future. Thank you for any help you can provide.

EDIT: Added solution below based on @roberto06's suggestion.

Jragyn
  • 29
  • 1
  • 7
  • 2
    `document.cookie.split('getQuoteObject=')[1].split(';')[0]` – Rajesh Oct 27 '16 at 08:01
  • What does `document.cookie.indexOf("language=")` return ? – roberto06 Oct 27 '16 at 08:04
  • @Rajesh How does that code know what cookie to get? There are multiple values in the cookie set by other scripts or plugins on the page. How does that script know that looking _only_ for the value for the name "language"? – Jragyn Oct 27 '16 at 08:06
  • You can only access cookies added by your domain. You an refer following post for more information: http://stackoverflow.com/questions/3363495/javascript-and-third-party-cookies – Rajesh Oct 27 '16 at 08:15
  • @roberto06 Thank you so much! That was exactly the kind of thing I was looking for. I'm sure it's "too simple" for many others, but it completely serves my purpose in a way I can understand and troubleshoot later if needed! – Jragyn Oct 27 '16 at 08:21

1 Answers1

0

@roberto06's suggestion did exactly what I needed in the simplest way! I rewrote it the following way:

var en = document.cookie.indexOf("language=EN");
var fr = document.cookie.indexOf("language=FR");
var ht = document.cookie.indexOf("language=HT");

The language that is set will now return a positive number (or a 0 in some tests, presumably if the value comes first in the cookie). The result for no match is -1. So then I just examine which resulting number is greater than or equal to 0 (there will only be one that is) and redirect based on that:

if (en >= 0) { window.location = "http://example.com/en"; } else
if (fr >= 0) { window.location = "http://example.com/fr"; } else
if (ht >= 0) { window.location = "http://example.com/ht"; };

If none of these values is greater than or equal to 0, that means no language value is set in the cookie, so the page does not redirect at all. It stays on the language selection page so the visitor can set their cookie! Thank you, @roberto06. I was trying those types of things but never got it quite right, apparently.

I'm sure this method is "too simple" for some people, but I like this way much better for my purposes. Thanks!

Jragyn
  • 29
  • 1
  • 7