I am making a landing page with a drop down menu which will redirect users to a new directory.
That part is working and now I need to add code to set a cookie when the country is selected, so that the next time the user visits the landing page they will automatically get redirected to that country's directory without having to select from the drop down menu again.
I agree with yckart's comment and suggest using localStorage.
The support is pretty good (shown here) and unless you need to support IE7 or less you should be ok.
You can set and retrive data like this:
localStorage.varName = 'bling'; var myVal = localStorage.varName; // or: localStorage['a little more flexibility'] = 'bling';
To use this, you could do the following:
An approach that would be a little more conservative would be to compare the value stored in localStorage (if there is one) to options in the select-list before doing the redirect, as shown in this jsFiddle:
©2020 All rights reserved.