cookie.js

Version 1.7

Overview

Description

The Cookie class procedures are called to get or set a website cookie. It's great for website customization without requiring a visitor to sign in.

The draw back to this is that a cookie will expire. That means that if a visitor doesn't visit the website before the cookie expires, the visitor will need to configure the setting(s) again.

Note of caution

Cookies are restrictive locally when using ports because they don't differentiate a cookie. That means that localhost:80 and localhost:5050 share cookies, so when cookies have the same name they will change each others' cookie. This architecture's lack of port support is surprising to me, and is why a development box can't run more than one website in a stable state.

Structure

The class name is Cookie.

Calling a procedure would look something like: Cookie.GetCookie(CookieName)

There are two customized functions that come from W3Schools, GetCookie and SetCookie. The rest are GetCookie() embedded variants.

Procedures

NOTE: All of the matching function parameters are of the same type.

  1. SetCookie(CookieName, CookieValue, CookieExpiresInDays)
  2. GetCookie(CookieName)
  3. GetCookieValue(CookieName, CookieDefaultValue)
  4. GetCookieValueAndSetDaily(CookieName, CookieDefaultValue)

Example setup

The following can easily be loaded with every page load, and will make it less likely for visitors to need to reconfigure all of their settings again. Simply add it to a script file, something like variables.js will work just fine.

var CookieMode = 'Mode';
var ModeDefault = 'day';
var ModeSetting = Cookie.GetCookieValueAndSetDaily( CookieMode, ModeDefault );

In the previous code CookieMode = CookieName, ModeDefault = CookieDefaultValue, and ModeSetting = CookieValue.

Procedure

1. SetCookie()

Self explanatory except for CookieExpiresInDays, which is the number of days before the cookie expires. 365 is used when GetCookieValueAndSetDaily() is called.

2. GetCookie()

This function can be used to get a cookie value.

3. GetCookieValue()

This will set a cookie to CookieDefaultValue when a cookie does not exist. CookieDefaultValue works with empty space ("") or null. If you don't need CookieDefaultValue, just use GetCookie().

4. GetCookieValueAndSetDaily()

This works the same as GetCookieValue(), and it will push the expiry date by another 365 days.