Jump to content
Larry Ullman's Book Forums

Errata || Using The Cookie Library || Renewing The Cookie


Recommended Posts

Hi,

 

I tested the example " Using the Cookie Library"... also I saw this in errata :

 

2e3o16p.png

 

and everything works fine, but I think that now the cookie is not renewed since the setTheme(theme) function does not create (set) any cookie. And, in the book there is the following text:

 

However, as written, the theme cookie will expire in a week. By calling the setThemeCookie() function here, the cookie will be renewed for another week (from today), and the page will be updated. This keeps the user’s preferences retained so long as it hasn’t been more than a week since the user accessed the page.

 

But, in eratta it is not explained how to renew the cookie. What would be the best and most elegant way to do this? This is how I did:

window.onload = function() {
	'use strict';
	document.getElementById('aTheme').onclick = setThemeCookie;
	document.getElementById('bTheme').onclick = setThemeCookie;
	var value = COOKIE.getCookie('theme');
	if (value) {
		setTheme(value);
                var expire = new Date();
		expire.setDate(expire.getDate() + 7);
		COOKIE.setCookie('theme', value, expire);
	}
};

The original code was:

window.onload = function() {
        'use strict';
        document.getElementById('aTheme').onclick = setThemeCookie;
        document.getElementById('bTheme').onclick = setThemeCookie;
        var theme = COOKIE.getCookie('theme');
        if (theme) {
                setTheme(theme);
        }
};

2. I have another question:

  • the cookie was created (for example, I clicked on the link 'aTheme');
  • then I browsed to some other website (for example, I opened google.com);
  • and, finnaly, I oppened the same example (page) again (by entering path in a browser).

Why theme is not remembered, CSS is not loaded? I need to click, to choose theme again to load CSS on the page? This applies to both - the code downloaded from this website, and for my code.

Link to comment
Share on other sites

 Share

×
×
  • Create New...