Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To Store RSS feed preference

Status
Not open for further replies.

210163

Programmer
Jun 15, 2008
7
NO
Hello all!

Right now I am trying to implement a function which makes it possibable for my users to save a preference on which news site they want to read news from. Check out: -- the function is not implementede yet, as you might have guessed.

I try to store preferences through cookies. But I face a problem when I want to store the second feed url, as I only overwrite the information I added with the first feed url. Can you show how to add values to a cookie which already exists, instead of overwriting it with new information?

The function which I currently use to write my cookies is this one I found in Dreamweaver:

function writeCookie(name, value, hours) {
var expire = "";
if(hours != null)
{
expire = new Date((new Date()).getTime() + hours * 3600000);
expire = "; expires=" + expire.toGMTString();
}
document.cookie = name + "=" + escape(value) + expire;
}

Thank you very much!
 
Hi there.

Here are a couple of simple functions you can add to your existing one... to allow you to store multiple values (using a key/pair scheme):
Code:
<script type="text/javascript">
// existing cookie writing code
function writeCookie(name, value, hours) {
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}

// this setter allows you to update the value of a key (or add the key/value pair if it is not already there)
function setCookieKeyData(name, key, value) {
	var data = document.cookie;
	if (data) {
		var bUpdated = false;
		var cookieKeyValueData = getCookieKeyData(name).split('|');
		for (var loop=0; loop<cookieKeyValueData.length; loop++) {
			if (cookieKeyValueData[loop].split(':')[0] == key) {
				cookieKeyValueData[loop] = key + ':' + escape(value);
				bUpdated = true;
				break;
			}
		}
		if (!bUpdated) {
			cookieKeyValueData[cookieKeyValueData.length] = key + ':' + value;
		}
		writeCookie(name, cookieKeyValueData.join('|'));
	}
}

// this getter allows you to get back the value of the key from a cookie called name
//  - returns all cookie data if no key is supplied
//  - returns null if the key supplied was not found
function getCookieKeyData(name, key) {
	if (name) {
		var data = document.cookie;
		if (key) {
			var cookieKeyValueData = unescape(data.split(name+'=')[1]).split('|');
			for (var loop=0; loop<cookieKeyValueData.length; loop++) {
				if (cookieKeyValueData[loop].split(':')[0] == key) {
					return cookieKeyValueData[loop].split(':')[1];
				}
			}
		} else {
			return unescape(data.split(name+'=')[1]);
		}
	}
	return null;
}
</script>

Create the cookie as the first step:

Code:
writeCookie('myPageCookie', '', 24);

Add in some key/value pairs of data using the setter setCookieKeyData():

Code:
setCookieKeyData('myPageCookie', 'key1', 123);
setCookieKeyData('myPageCookie', 'key2', 456);
setCookieKeyData('myPageCookie', 'key3', 'banana');

Alert the contents of specific key data using the getter getCookieKeyData():

Code:
alert(getCookieKeyData('myPageCookie', 'key1'));     // 123
alert(getCookieKeyData('myPageCookie', 'key2'));     // 456
alert(getCookieKeyData('myPageCookie', 'key3'));     // banana

The code is not bullet-proof, it ought to allow you to see how you might do this.

You should also look into JSON (which could be a better alternative - depending on your needs).

Cheers,
Jeff

[tt]Visit my blog [!]@[/!] Visit Code Couch [!]@[/!] [/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Thanks!

Can I store my preferences in JSON? I have no experience with it, but if I can improve my system I will try to learn it. Could you explain JSON´s benefits over my current system with cookies? Give me an outline of how it could be done?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top