How to Set a Cookie When Changing the Language of the Website
Javascript provides you with a quick way to set a cookie when a user chooses a language on your site. This means you can detect the selected user language the next time the visitor navigates to your web pages. Setting language settings is called localization, and it improves global usability for your website readers. Javascript supports any language website including PHP, HTML, VB.NET or C#. This makes it easy for any developer to implement language cookies.
Instructions
-
-
1
Create the expiration date for the coffee. When the expiration date is reached, the browser deletes the cookie. The next time the user accesses the site, a new cookie is set. The following code creates a cookie that lasts 90 days on the user's browser:
var expire_date =new Date();
expire_date.setDate(expire_date.getDate()+90); -
2
Create the cookie name. You can name the cookie any recognizable name. The following code creates a Javascript variable for the cookie name:
var cookie_name = "Language";
-
-
3
Create the cookie and set the language. You can use the standard characters for each language (see Resources), but you can use any customized standard for your cookie. The following code sets a language cookie for your user:
document.cookie=cookie_name + "=" + "en"
The code above sets the cookie to "English" using the two-character abbreviation "en."
-
1
References
Resources
- Photo Credit cookie image by Vera Kailova from Fotolia.com